使用Socket.IO在Python客户端和nodeJS服务器之间进行通信

6
我正在尝试使用socket.io将从树莓派(python 2.7.9)发送的值发送到我的nodeJS服务器。
我的目标是通过websocket连接连续地发送多个值,传递给我的本地node服务器,它应该将这些值显示在index.html上(用于其他客户端,例如只有树莓派发送值的Web-Chat)。

I tried everything but I can't make a handshake and send data. When I open the "http://IP_ADDRESS:8080" in my browser I see a connection but not with my python Code.

Please I need some help....

server.js

var express = require('express')
,   app = express()
,   server = require('http').createServer(app)
,   io = require('socket.io').listen(server)
,   conf = require('./config.json');

// Webserver
server.listen(conf.port);

app.configure(function(){

    app.use(express.static(__dirname + '/public'));
});


app.get('/', function (req, res) {

    res.sendfile(__dirname + '/public/index.html');
});

// Websocket
io.sockets.on('connection', function (socket) {

    //Here I want get the data
    io.sockets.on('rasp_param', function (data){
        console.log(data);
    });

    });
});

// Server Details
console.log('Ther server runs on http://127.0.0.1:' + conf.port + '/');

my python websocket-code in which I just want send values

#!/usr/bin/env python
#

from websocket import create_connection

ws = create_connection("ws://IP_ADDRESS:8080/")
ws.send("Some value")
ws.close();


当你说 http://IP_ADDRESS:8080 中的 IP_ADDRESS 是 127.0.0.1 还是与树莓派连接到同一网络的地址? - Will
是的 :) 它在同一网络中。 - Samy
我遇到了同样的问题。我正在使用这个库:https://pypi.python.org/pypi/socketIO-client 我可以连接到Python SocketIO服务器:https://pypi.python.org/pypi/python-socketio但是我无法连接到Node.js Socket.IO服务器。有什么帮助吗? - Mike
1个回答

2

Socket.io通信不是纯粹的Websockets。您可能需要在Python上实现socket.io客户端,以确保您发送的消息与socket.io协议兼容。例如socketIO-client


2
我遇到了同样的问题。我正在使用这个库:https://pypi.python.org/pypi/socketIO-client 我可以连接到Python SocketIO服务器:https://pypi.python.org/pypi/python-socketio但是我无法连接到Node.js Socket.IO服务器。有什么帮助吗? - Mike

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接