问题描述
我想将NodeMCU Lua套接字客户端连接到node.js socket.io服务器.
I want to connect a NodeMCU Lua socket client to node.js socket.io server.
NodeMCU Lua代码:
sk = net.createConnection(net.TCP, 0)
sk:on("receive", function ( sck,c )
print (c)
end)
sk:on("connection", function ( sck,c )
print("Connected")
sk:send("Helloooo...")
end)
sk:connect(12346,"192.168.1.100")
Node.js服务器代码:
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('someone is connected');
});
server.listen(12346);
问题:
Lua客户端中的on连接事件被触发并显示已连接",但是node.js socket.io服务器中的on连接事件未被触发.我用Python套接字服务器尝试了Lua客户端,它运行良好!而且我还尝试了使用Javascript套接字客户端的node.js套接字服务器,并且运行良好!
The on connection event in the Lua client is fired and prints "Connected", but the on connection event in node.js socket.io server isn't fired. I tried the Lua client with a Python socket server and it worked well! And I also tried a node.js socket server with a Javascript socket client and it worked well!
NodeMCU和socket.io之间是否存在兼容性问题?
Are there compatibility problems between NodeMCU and socket.io?
推荐答案
Socket.io 是 WebSocket 包装器,不是基本的套接字实现.其中有一些特定的操作,例如握手和心跳.因此,您可以使用套接字服务器成功,但不能使用WebSocket服务器.
Socket.io is a WebSocket wrapper, not a basic socket implementation. There are some specific operations in it such as handshaking and heartbeat. So you can succeed with socket servers but not with a WebSocket one.
您也可以在 NodeMCU 上使用WebSocket客户端实现侧面.但是我不确定Lua库是否与WebSocket API版本匹配.
You may use a WebSocket client implementation as well on the NodeMCU side. But I am not sure if the Lua library matches with the WebSocket API version.
如果您想要异步通信,则可以使用 MQTT ,该库也有很多库用于NodeJS.否则,请像以前一样成功使用 NodeJS 的套接字服务器.
If you want async communication, you may use MQTT, which has also lots of libraries for NodeJS. Otherwise use the socket server of NodeJS as you have done successfully previously.
这篇关于将NodeMCU Lua套接字客户端与node.js socket.io服务器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!