gpt4 book ai didi

javascript - 需要升级 (node.js)

转载 作者:行者123 更新时间:2023-11-30 00:01:59 25 4
gpt4 key购买 nike

我使用 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

最佳答案

看起来您正在尝试在同一个脚本中创建 WebSockets 客户端和服务器。

/signaling.js:26
let ws = new WebSocket(wsUrl);
^

ReferenceError: WebSocket is not defined

发生此错误是因为您忘记在文件顶部定义 WebSocket 变量:

var WebSocket = require('ws');

由于您想使用与在 Web 浏览器中使用的方法相同的方法,(ws.onopenws.onerror、...),我会建议您改用 websocket 包和 w3cwebsocket 模块。

var WebSocket = require('websocket').w3cwebsocket;

在评论中讨论了这个问题后,我建议您完全删除 'ws' 包并改用 'websocket'

var WebSocketServer = require('websocket').server;

与其直接创建 WebSocket 服务器,不如先创建一个 HTTP 服务器:

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) {

关于javascript - 需要升级 (node.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100317/

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