Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Client to nodeJS Server with Socket.IO

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

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)

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();
like image 728
Samy Avatar asked Nov 08 '22 11:11

Samy


1 Answers

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.

like image 162
dvlsg Avatar answered Nov 14 '22 21:11

dvlsg