【发布时间】:2018-08-11 21:08:40
【问题描述】:
我制作了一个程序来连接我的 java 程序,该程序使用套接字将数据发送到我的 nodejs 服务器,并且 nodejs 服务器应该使用 socket.io 将接收到的数据发送到浏览器,但是我确实从那里接收数据存在问题java 但节点服务器没有将它发送到浏览器这里是代码
// Create an instance of the Server and waits for a connexion
net.createServer(function(sock) {
// Receives a connection - a socket object is associated to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
// Add a 'data' - "event handler" in this socket instance
sock.on('data', function(data) {
//data was received in the socket and converting it into string
var textChunk = data.toString('utf8');
io.emit('message', textChunk); //socket.io is supposed to send the data to the browser
console.log(textChunk);
});
// Add a 'close' - "event handler" in this socket instance
sock.on('close', function(data) {
// closed connection
console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
});
}).listen(PORT, HOST);
【问题讨论】:
-
我该怎么做?
标签: javascript java node.js websocket socket.io