【问题标题】:Python Client to nodeJS Server with Socket.IO带有 Socket.IO 的 Python 客户端到 nodeJS 服务器
【发布时间】:2016-08-09 18:52:24
【问题描述】:

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

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

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

我需要一些帮助....

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 + '/');

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

#!/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:8080IP_ADDRESS 127.0.0.1 还是你的pi 连接到的同一网络上的地址?
  • 是的 :) 它在同一个网络中
  • 我遇到了同样的问题。我正在使用这个库:pypi.python.org/pypi/socketIO-client 我可以连接到 python socketIO 服务器:pypi.python.org/pypi/python-socketio 但我无法连接到 nodejs socket.io 服务器有什么帮助吗?

标签: python node.js websocket socket.io


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-04-23
  • 2022-01-16
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多