使用 Socket.IO 到 nodeJS 服务器的 Python 客户端

Sam*_*amy 5 python websocket node.js socket.io

我正在尝试使用 socket.io 将值从我的树莓派(在 python 2.7.9 中)发送到我的 nodeJS 服务器。

我的目标是通过 websocket 连接将许多值从我的 pi 连续发送到我的节点服务器(本地),它应该获取这些值并将其显示在 index.html 上(对于其他客户端,例如只有树莓派发送的网络聊天)值)

我尝试了所有方法,但无法握手和发送数据。当我在浏览器中打开“ http://IP_ADDRESS:8080 ”时,我看到了一个连接,但没有与我的 python 代码连接。

请我需要一些帮助......

服务器.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 + '/');
Run Code Online (Sandbox Code Playgroud)

我的 python websocket - 代码,我只想在其中发送值

#!/usr/bin/env python
#

from websocket import create_connection

ws = create_connection("ws://IP_ADDRESS:8080/")
ws.send("Some value")
ws.close();
Run Code Online (Sandbox Code Playgroud)

dvl*_*lsg 2

Socket.io 通信不是普通的 websocket。您可能需要在 python 上实现 socket.io 客户端,以确保您发送的消息与 socket.io 协议兼容。也许是类似于socketIO-client 的东西。

  • 我有同样的问题。我正在使用这个库:https://pypi.python.org/pypi/socketIO-client 我可以连接到python socketIO服务器:https://pypi.python.org/pypi/python-socketio 但我无法连接到nodejs socket.io 服务器 有帮助吗? (2认同)