Socket.IO 控制字符问题

发布于 2024-12-01 12:13:19 字数 2636 浏览 1 评论 0原文

我正在实现一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追我者格杀勿论 2024-12-08 12:13:19

已在 socket.io v0.8.6 中修复。应该回答/关闭这个问题。

Fixed with socket.io v0.8.6. This q should be answered/closed.

栖竹 2024-12-08 12:13:19

这只是需要在本地动态分配内存的情况。当字符串进来时,您应该将字符串复制到动态分配的相同长度的字符串中。问题解决了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文