0

So this is what I am trying to do, I want to be able to send a message in a browser towards a python script. I've got to the point where I can send a message in the browser and the server sees it. For testing purposes I used io.emit('input', data) to send the data towards my python script but nothing happens on the python side.

script.py:

import socketio

sio = socketio.Client()

@sio.event
def connect():
    print('connected')

@sio.on("input")
def on_input(key):
    print(key)

sio.connect('http://192.168.2.9:5000', namespaces=['/justin'])

server.js:

const express = require('express')
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
var justin = null;

app.use(express.static('public'));

io.of('/justin').on('connection', function(socket){
  console.log('justin connected');
  justin = socket;
});

io.on('connection', function(socket){
  console.log('a user connected');
  socket.on('event', (data) => {
    io.emit('input', data)
  })

  socket.on('disconnect', () => {
    //
  })
});



http.listen(5000, function(){
  console.log('listening on *:5000');
});

Is there something I'm not seeing or is this just not possible? Thanks in advance!

Wouter
  • 11
  • 1
  • 6
  • The JavaScript code you provided is not running in the browser, but rather in a NodeJS server program. If I understood properly, you want to let the user of your website send a message (with HTML + JavaScript) to your NodeJS server (`server.py`), which then transmits it to a Python script (`script.py`) on the same server. Is that correct? – Pierre Nov 03 '19 at 12:36
  • No the server is running in NodeJS. I want to have the browser as a client as well as the python script. – Wouter Nov 03 '19 at 16:32
  • Your server listens on the `/` and `/justin` namespaces, but your client only uses `/justin`. Any reason for that discrepancy? – Miguel Grinberg Nov 04 '19 at 05:11
  • I want to know which one is the python script client and which one the browser client. So I gave the python script a /justin namespace. – Wouter Nov 04 '19 at 07:59
  • Then your `@sio.event` and `@sio.on` decorators should define the `/justin` namespace. You are basically connecting on `/justin` but defining event handlers for `/` with the code you posted in your question. – Miguel Grinberg Nov 05 '19 at 04:24
  • shouldn't `io.emit()` emit to all connected clients? – Wouter Nov 05 '19 at 06:35

0 Answers0