gpt4 book ai didi

node.js - 如何在 WebSocket 上生成 "room"

转载 作者:太空宇宙 更新时间:2023-11-04 02:21:57 25 4
gpt4 key购买 nike

我正在使用WebRTC“库”,它允许用户通过点对点连接传输音频、视频等。为此,我需要在 Internet Explorer 中通过 WebSocket 启动连接。

我选择了 heroku.com 作为我的 Node.js 托管。

这是我的服务器文件,我需要将用户添加到“房间”,但它崩溃了,如下所示:

var WebSocketServer = require("ws").Server;
var http = require("http");
var cool = require('cool-ascii-faces');
var express = require('express');
var app = express();
var port = process.env.PORT || 5000;
app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/', function(request, response) {
response.render('pages/index')
});

app.get('/cool', function(request, response) {
response.send(cool());
});

app.use(express.static(__dirname + "/"))

var server = http.createServer(app)
server.listen(port)

console.log("http server listening on %d", port)

var wss = new WebSocketServer({server: server})
console.log("websocket server created")

wss.on("connection", function(ws) {
console.log("websocket connection open")

///////////////
// I NEED HELP HERE
ws.on('message', function(room) {
console.log("I am trying to create room: " + room);

ws.join(room); // this line is crashing
// with error "There is NO function join"


});

ws.on("close", function() {
console.log("websocket connection close")
})
})

这是我的客户端,我在其中发送了我想要创建的房间的名称:

var host = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(host);
waitForSocketConnection(ws);
ws.onmessage = function (event) {
console.log('got ' + event);
console.log('got2 ' + event.data);
};

function waitForSocketConnection(socket){
setTimeout(
function(){
if (socket.readyState === 1) {
socket.send("Lukasz30M");
return;
} else {
waitForSocketConnection(socket);
}
}, 5);
};

这是 WebRTC 提供的示例,我正在尝试遵循:

var static = require('node-static');
var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function (req, res) {
file.serve(req, res);
}).listen(2013);

var io = require('socket.io').listen(app);

io.sockets.on('connection', function (socket){

// convenience function to log server messages on the client
function log(){
var array = [">>> Message from server: "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}

socket.on('message', function (message) {
log('Got message:', message);
// for a real app, would be room only (not broadcast)
socket.broadcast.emit('message', message);
});

socket.on('create or join', function (room) {
var numClients = io.sockets.clients(room).length;

log('Room ' + room + ' has ' + numClients + ' client(s)');
log('Request to create or join room ' + room);

if (numClients === 0){
socket.join(room);
socket.emit('created', room);
} else if (numClients === 1) {
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
} else { // max two clients
socket.emit('full', room);
}
socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);

});

});

谢谢你的指点。我完全停止了;)

最佳答案

wrtc 不准许大学和 pagine 不 sicure。 devi avere un collegamento https。

<小时/>

WRTC 不允许链接到非安全页面。您必须有 https 连接

关于node.js - 如何在 WebSocket 上生成 "room",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32956092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com