Socket.IO 控制字符问题
我正在实现一个使用 websockets 和通过 Telnet 访问的控制台的应用程序。通过 websocket 建立的连接与控制台之间存在通信。我遇到一个奇怪的问题:
- 如果我在控制台中输入某些内容时将字符串常量发送到已建立的套接字,则它可以正常工作。
- 如果我发送从控制台范围收到的字符串,它似乎会打开一个新的套接字(不确定),因为在调试日志中我看到它,并且在浏览器端(websockets)它会提醒我有一个新连接。
- 如果我发送本地字符串(而不是从其他范围收到的字符串),则它会正确发送。 (注释行:client.send(message))
我在这里分享nodeJS代码,考虑到这现在是一个测试应用程序,所以假设只有一个套接字和websockets连接:
// Sample based on: http://elegantcode.com/2011/05/04/taking-baby-steps-with-node-js-websockets/
// Changed for sockets.io 6.x => 7.x
var events = require('events');
var eventEmitter = new events.EventEmitter();
var http = require('http');
var socketIO = require('socket.io');
var static = require('node-static');
var port = 2000;
var clientFiles = new static.Server('./client');
var httpServer = http.createServer(
function(request, response) {
request.addListener('end', function() {
clientFiles.serve(request, response);
});
})
httpServer.listen(port);
console.log("Server running at port: " + port);
var io = require('socket.io').listen(httpServer);
var webSocket = io.sockets;
webSocket.on('connection', function(client) {
client.send('Please enter a user name ...');
var userName;
eventEmitter.on('someOccurence', function(message) {
console.log("Received: " + message);
client.send('Line Received');
var s = 'popo';
client.send(s);
//client.send(message);
});
client.on('message', function(message) {
console.log(message);
if(!userName) {
userName = message;
client.broadcast.send(message + ' has entered the zone.');
return;
}
var broadcastMessage = userName + ': ' + message;
client.broadcast.send(broadcastMessage);
client.send("Repeated here: " + message);
});
client.on('disconnect', function() {
var broadcastMessage = userName + ' has left the zone.';
client.broadcast.send(broadcastMessage);
});
});
var lines = require('lines-adapter');
var net = require('net');
var server = net.createServer(function (socket) {
socket.write("Welcome to the Server\r\n");
lines(socket, 'utf8')
.on("data",
function(line) {
console.log("Line received: " + line);
eventEmitter.emit('someOccurence', line);
}
)
.end("end",
function() {
console.log("End of Stream");
}
);
});
server.listen(1337, "127.0.0.1");
更新:我刚刚测试过使用socket.io 0.8.6和nodeJS 0.4.12没有这个问题。我将这个问题保留在这里以供将来参考。
I am implementing an application that uses websockets and a console accessed via Telnet. There is a communication between the connection established via websockets and the console. I am experiencing a weird issue:
- If I send a string constant to an established socket when something is entered in the console it works ok.
- If I send a string received from the console scope It seems to open a new socket (not sure) because in the debug log I see it and in the browser side (websockets) it alerts me of a new connection.
- If I send a local string (instead of the one received from the other scope) it's sent correctly. (commented line: client.send(message) )
I share here the nodeJS code, take into account that this is now a test app so it's assumed only one socket and websockets connection:
// Sample based on: http://elegantcode.com/2011/05/04/taking-baby-steps-with-node-js-websockets/
// Changed for sockets.io 6.x => 7.x
var events = require('events');
var eventEmitter = new events.EventEmitter();
var http = require('http');
var socketIO = require('socket.io');
var static = require('node-static');
var port = 2000;
var clientFiles = new static.Server('./client');
var httpServer = http.createServer(
function(request, response) {
request.addListener('end', function() {
clientFiles.serve(request, response);
});
})
httpServer.listen(port);
console.log("Server running at port: " + port);
var io = require('socket.io').listen(httpServer);
var webSocket = io.sockets;
webSocket.on('connection', function(client) {
client.send('Please enter a user name ...');
var userName;
eventEmitter.on('someOccurence', function(message) {
console.log("Received: " + message);
client.send('Line Received');
var s = 'popo';
client.send(s);
//client.send(message);
});
client.on('message', function(message) {
console.log(message);
if(!userName) {
userName = message;
client.broadcast.send(message + ' has entered the zone.');
return;
}
var broadcastMessage = userName + ': ' + message;
client.broadcast.send(broadcastMessage);
client.send("Repeated here: " + message);
});
client.on('disconnect', function() {
var broadcastMessage = userName + ' has left the zone.';
client.broadcast.send(broadcastMessage);
});
});
var lines = require('lines-adapter');
var net = require('net');
var server = net.createServer(function (socket) {
socket.write("Welcome to the Server\r\n");
lines(socket, 'utf8')
.on("data",
function(line) {
console.log("Line received: " + line);
eventEmitter.emit('someOccurence', line);
}
)
.end("end",
function() {
console.log("End of Stream");
}
);
});
server.listen(1337, "127.0.0.1");
UPDATE: I just tested with socket.io 0.8.6 and nodeJS 0.4.12 without this issue. I will keep this question here for future reference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已在 socket.io v0.8.6 中修复。应该回答/关闭这个问题。
Fixed with socket.io v0.8.6. This q should be answered/closed.
这只是需要在本地动态分配内存的情况。当字符串进来时,您应该将字符串复制到动态分配的相同长度的字符串中。问题解决了。
This is just a case where you need to dynamically allocate memory locally. When the string comes in you should do a string copy of the string into a dynamically allocated string of the same length. problem solved.