Jat*_*tin 5 sockets tcp node.js socket.io
我有这个简单的TCP服务器:
var net = require('net');
var server = net.createServer(function (socket) {
socket.on('connect', function() {
console.log("New client!");
});
});
server.listen(8000, function(){
console.log("server running...")
});
Run Code Online (Sandbox Code Playgroud)
然后我有另一个文件作为client.js:
var net = require('net');
var client = net.connect({port: 8000},
function() {
console.log('client connected');
});
client.on('error', console.error);
Run Code Online (Sandbox Code Playgroud)
我在一个终端窗口中运行服务器,然后在其他窗口中运行客户端,并期望看到服务器日志"新客户端".虽然,这不会发生.那么,'connect'事件何时准确发出?
Joa*_*son 17
net.createServer将给定函数设置为connection事件的侦听器.
换句话说,在服务器端,当您获得回调时,套接字已经连接,并且您尝试侦听的事件不会在已连接的套接字上发出.
| 归档时间: |
|
| 查看次数: |
7085 次 |
| 最近记录: |