Javascript 使用WebSocket在Lua和NodeJ之间通信不起作用
我正在为我的Minecraft采矿海龟制作一个控制面板,我需要使用WebSocket在两者之间进行通信。我已经解决了Lua方面的问题,当我将它连接到echo服务器时,它可以正常工作 代码如下:Javascript 使用WebSocket在Lua和NodeJ之间通信不起作用,javascript,node.js,websocket,lua,Javascript,Node.js,Websocket,Lua,我正在为我的Minecraft采矿海龟制作一个控制面板,我需要使用WebSocket在两者之间进行通信。我已经解决了Lua方面的问题,当我将它连接到echo服务器时,它可以正常工作 代码如下: local ws,err = http.websocket("wss://localhost:5757") if ws then ws.send("Hello") print(ws.receive()) ws.close() end co
local ws,err = http.websocket("wss://localhost:5757")
if ws then
ws.send("Hello")
print(ws.receive())
ws.close()
end
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 5757 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('testing 123');
});
然而,我无法让NodeJS一方工作。我尝试过不同的端口,我尝试过打开端口
代码如下:
local ws,err = http.websocket("wss://localhost:5757")
if ws then
ws.send("Hello")
print(ws.receive())
ws.close()
end
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 5757 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('testing 123');
});
我想不出哪里出了错。感谢所有的帮助
编辑:我想补充一点,它也没有给出错误,我正在使用ws-npm包并运行最新的LTS节点版本
编辑2:我在这里尝试了这个代码示例
const WebSocket = require('ws');
const ws = new WebSocket('wss://echo.websocket.org/');
ws.on('open', function open() {
ws.send('testing 123');
});
ws.on('message', function incoming(data) {
console.log(data);
});
它工作了,并且回复为“testing 123”,因此web套接字似乎不想在本地主机上运行。您使用的是wss而不是ws。。。这意味着web套接字将在nodejs端失败,因为它无法加密消息(类似于https)。正确的url应该是
ws://localhost:port
您使用的是wss而不是ws。。。这意味着web套接字将在nodejs端失败,因为它无法加密消息(类似于https)。正确的url应该是ws://localhost:port