Reputation: 189
I have created a python tcp server and i am using node.js socketio module to communicate between browser and tcp server. Hierarchically they are connected like: Browser->Node.js->Python TCP Server Browser<-Node.js<-Python Tcp Server I can connect with Node.js to Tcp Server, i can send messages but right after i send the first message i cant send any message from Node.js to TCP Server, similarly after i send a message from TCP Server to Node.js i cant send messages after the first message. My Node.js code:
var http = require('http');
var mysql = require('mysql');
var net = require('net');
var socketio = require('socket.io');
////////////////////////////////////////////////////////////////
// tcp socket
// Connection part to my python server
////////////////////////////////////////////////////////////////
var tcpClient = net.connect({port: 3001, host:"localhost"});
tcpClient.setEncoding("utf8");
tcpClient.on('connect',function() {
console.log('client connected');
tcpClient.write('node - tcp server a bağlanıldı!');
});
tcpClient.on('data', function(data) {
console.log("tcp den gelen mesaj = "+ data.toString());
});
tcpClient.on('end', function() {
console.log('client disconnected');
});
tcpClient.on('error', function(data) {
console.log("error = " + data);
});
////////////////////////////////////////////////////////////////
// web socket
////////////////////////////////////////////////////////////////
var io = socketio.listen(3000);
io.sockets.on('connection', function (socket) {
socket.on('sendMessage', function (data) {
tcpClient.write("node - sendMessage");
console.log(data);
});
socket.on('login', function (data) {
tcpClient.write('node - login');
console.log(data);
});
});
I have checked my Python TCP Server with Python Client which i coded and everything seems alright. What might be the problem? Thanks in advance.
Upvotes: 0
Views: 1810
Reputation: 11266
You are essentially proxying the events from the browser up to the node and onward to python, and vice versa -- which means your browser has to emit socket events to node, and then when node listens to the socket event, has to emit another event up to python.
That's tricky, but not impossible.
Browser NodeJS Python
CLIENT => SERVER/CLIENT <= SERVER
Try to get it working with one pair of client <=> servers, and then the other pair, and then work on getting the socket events forwarded last.
This is very javascript-pseudo-code, but it's just to illustrate the idea:
//on your Browser - some jQuery
$('#login').on('click', function(){
NodeSocket.emit('login');
}
//on your server.js/app.js
NodeSocket.on('login', function(){
//Note that we are forwarding this event to Python
PythonSocket.emit('login');
});
//on your server.py ~whatever the python equivalent of this function is~
PythonSocket.on('login', function(){
//do login stuff
//Note that we are emitting an event back to Node
NodeSocket.emit('loginSuccess');
});
//on your server.js/app.js
NodeSocket.on('loginSuccess', function(){
//Note that we are forwarding this event to the Browser
BrowserSocket.emit('loginSuccess');
});
//on your browser
BrowserSocket.on('loginSuccess', function(){
console.log("Login Success!");
}
Some other thoughts:
(1) Based on your code it looks like you're only writing to the socket on the connect event
tcpClient.on('connect',function() {
console.log('client connected');
tcpClient.write('node - tcp server a bağlanıldı!');
});
(2) Look at how/where youre emitting your socket events::
socket.on('sendMessage'...
socket.on('login'...
these are custom events that have to be emitted from the client via
socket.emit('customEvent')
in order for the server to listen to them.
If the client doesnt emit the socket event, the server will never call the event handler.
Upvotes: 1