【发布时间】: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:8080是IP_ADDRESS127.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