我使用webRTC制作了信令服务器。 这就是我制作信令服务器(signaling.js)的原因。
我该怎么做才能解决这个问题?
·signaling.js
"use strict";
let WebSocketServer = require('ws').Server;
let port = 3001;
let wsServer = new WebSocketServer({ port: port });
console.log('websocket server start. port=' + port);
wsServer.on('connection', function(ws) {
console.log('-- websocket connected --');
ws.on('message', function(message) {
wsServer.clients.forEach(function each(client) {
if (isSame(ws, client)) {
console.log('- skip sender -');
}
else {
client.send(message);
}
});
});
});
function isSame(ws1, ws2) {
// -- compare object --
return (ws1 === ws2);
} let wsUrl = 'ws://localhost:3001/';
let ws = new WebSocket(wsUrl);
ws.onopen = function(evt) {
console.log('ws open()');
};
ws.onerror = function(err) {
console.error('ws onerror() ERR:', err);
};
ws.onmessage = function(evt) {
console.log('ws onmessage() data:', evt.data);
let message = JSON.parse(evt.data);
if (message.type === 'offer') {
// -- got offer ---
console.log('Received offer ...');
textToReceiveSdp.value = message.sdp;
let offer = new RTCSessionDescription(message);
setOffer(offer);
}
else if (message.type === 'answer') {
// --- got answer ---
console.log('Received answer ...');
textToReceiveSdp.value = message.sdp;
let answer = new RTCSessionDescription(message);
setAnswer(answer);
}
};
function sendSdp(sessionDescription) {
console.log('---sending sdp ---');
textForSendSdp.value = sessionDescription.sdp;
/*--- テキストエリアをハイライトするのを止める
textForSendSdp.focus();
textForSendSdp.select();
----*/
// --- シグナリングサーバーに送る ---
let message = JSON.stringify(sessionDescription);
console.log('sending SDP=' + message);
ws.send(message);
}
当我加载“signaling.js”时,我设置了node.js. 之后,我访问signaling.js。 但是,它列出了所需的升级。
我搜索了如何升级node.js。
我试过
Linux/Mac:
The module n makes version-management easy:
sudo npm install n -g
sudo n 0.12.2
For the latest stable version:
sudo n stable
For the latest version:
sudo n latest
之后,我访问了signaling.js
/signaling.js:26
let ws = new WebSocket(wsUrl);
^
ReferenceError: WebSocket is not defined
at Object.<anonymous> (/Users/Desktop/webRTC/signaling.js:26:16)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
答案 0 :(得分:1)
看起来您正在尝试在同一个脚本中创建WebSockets客户端和服务器。
/signaling.js:26
let ws = new WebSocket(wsUrl);
^
ReferenceError: WebSocket is not defined
发生此错误是因为您忘记在文件顶部定义WebSocket
变量:
var WebSocket = require('ws');
由于您希望使用与在网络浏览器中使用相同的方法(ws.onopen
,ws.onerror
,...),我建议您使用websocket
而是使用w3cwebsocket
模块打包。
var WebSocket = require('websocket').w3cwebsocket;
在评论中讨论了问题后,我建议您完全删除'ws'
包,然后使用'websocket'
。
var WebSocketServer = require('websocket').server;
您应该首先创建HTTP服务器,而不是直接创建WebSocket服务器:
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
然后在其上附加WebSocket服务器:
wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
新连接会触发request
事件,您必须接受传入连接:
// wsServer.on('connection', function(ws) {
wsServer.on('request', function(request) {
var ws = request.accept();
您将能够使用connections
属性检索客户列表。
// wsServer.clients.forEach(function each(client) {
wsServer.connections.forEach(function each(client) {