Connecting NodeMCU Lua socket client with node.js socket.io server

。_饼干妹妹 提交于 2020-01-05 04:24:11

问题


I want to connect a NodeMCU Lua socket client to node.js socket.io server.

NodeMCU Lua code:

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 server code:

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

The problem:

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!

Are there compatibility problems between NodeMCU and socket.io?


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/41379538/connecting-nodemcu-lua-socket-client-with-node-js-socket-io-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!