使用 Socket.IO 到 nodeJS 服务器的 Python 客户端 [英] Python Client to nodeJS Server with Socket.IO

查看:33
本文介绍了使用 Socket.IO 到 nodeJS 服务器的 Python 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send values from my raspberry pi (in python 2.7.9) to my nodeJS server with socket.io.

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

My goal ist send many values continuously from my pi over a websocket connection to my node Server (local), which should take the values and show it on the index.html (for other clients, like a Web-Chat where just the raspberry sends values)

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

请我需要帮助....

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

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

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

推荐答案

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

Socket.io communications aren't plain websockets. You probably need an implementation of the socket.io client on python, to make sure that the messages you are sending are compatible with the socket.io protocol. Something like socketIO-client, maybe.

这篇关于使用 Socket.IO 到 nodeJS 服务器的 Python 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆