Initial commit
This commit is contained in:
53
robot/PythonSender.js
Executable file
53
robot/PythonSender.js
Executable file
@@ -0,0 +1,53 @@
|
||||
|
||||
module.exports = class PythonSender {
|
||||
|
||||
|
||||
constructor(p = 9875){
|
||||
const WebSocket = require('ws');
|
||||
|
||||
|
||||
this.sockets = [];
|
||||
this.server = new WebSocket.Server({port: p});
|
||||
|
||||
this.server.on('connection', (socket) => this.connectThis(socket));
|
||||
}
|
||||
|
||||
msgThis(msg){
|
||||
console.log("message:" + msg.toString())
|
||||
this.sockets.forEach(s => s.send(msg));
|
||||
}
|
||||
|
||||
closeThis(socket){
|
||||
console.log("Python Receiver is closing the connection");
|
||||
this.sockets = this.sockets.filter(s => s !== socket);
|
||||
}
|
||||
connectThis(socket) {
|
||||
console.log("Python Receiver is connected")
|
||||
this.sockets.push(socket);
|
||||
|
||||
// When you receive a message, send that message to every socket.
|
||||
socket.on('message', (msg) => this.msgThis(msg));
|
||||
|
||||
|
||||
// When a socket closes, or disconnects, remove it from the array.
|
||||
socket.on('close', () => this.closeThis(socket));
|
||||
}
|
||||
|
||||
send(m){
|
||||
if((this.sockets.length) > 0){
|
||||
this.sockets.forEach( s => s.send(m));
|
||||
}
|
||||
else{ console.warn("No sockets connected. Can't send the Message.");}
|
||||
}
|
||||
/*
|
||||
if(this.pythonSender != null){
|
||||
var strJSON = CANCreator.cmdMovement(this.motorPositionOld, this.motorPosition);
|
||||
if(strJSON != '{"message":"empty"}'){
|
||||
this.pythonSender.send(strJSON);
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user