简体   繁体   English

无法在TCP Server和Node.js之间发送消息

[英]Cant send messages between TCP Server and Node.js

I have created a python tcp server and i am using node.js socketio module to communicate between browser and tcp server. 我已经创建了一个python tcp服务器,并且我正在使用node.js socketio模块在浏览器和tcp服务器之间进行通信。 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. 从层次上来说,它们的连接方式如下:Browser-> Node.js-> Python TCP Server Browser <-Node.js <-Python Tcp Server我可以将Node.js连接到Tcp Server,我可以发送消息,但是在发送第一个消息之后消息我无法将任何消息从Node.js发送到TCP Server,类似地,在我将消息从TCP Server发送到Node.js之后,我也无法在第一条消息之后发送消息。 My Node.js code: 我的Node.js代码:

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. 我已经用我编写的Python Client检查了我的Python TCP Server,一切似乎都还不错。 What might be the problem? 可能是什么问题? Thanks in advance. 提前致谢。

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. 本质上,您是在将浏览器中的事件代理到节点,再到python,反之亦然-这意味着您的浏览器必须向节点发出套接字事件,然后当节点侦听套接字事件时,必须发出另一个事件取决于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: 这是非常javascript的伪代码 ,但这只是为了说明这个想法:

//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 (1)根据您的代码,看起来您只是在connect事件上写入套接字

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:: (2)查看您发出套接字事件的方式/位置:

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. 如果客户端不发出套接字事件,则服务器将永远不会调用事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM