IT TIP

제어 문자의 Socket.IO 문제

itqueen 2020. 11. 28. 13:23
반응형

제어 문자의 Socket.IO 문제


웹 소켓과 Telnet을 통해 액세스하는 콘솔을 사용하는 응용 프로그램을 구현하고 있습니다. 웹 소켓을 통해 설정된 연결과 콘솔간에 통신이 있습니다. 이상한 문제가 발생했습니다.

  • 콘솔에 무언가를 입력했을 때 설정된 소켓에 문자열 상수를 보내면 정상적으로 작동합니다.
  • 콘솔 범위에서받은 문자열을 보내면 디버그 로그에서 볼 수 있고 브라우저 측 (웹 소켓)에서 새 연결에 대해 경고하기 때문에 새 소켓을 여는 것 같습니다 (확실하지 않음).
  • 다른 범위에서받은 문자열 대신 로컬 문자열을 보내면 올바르게 전송됩니다. (주석 : client.send (message))

여기서 nodeJS 코드를 공유하고 이것이 이제 테스트 앱이므로 하나의 소켓과 웹 소켓 연결 만 가정한다는 점을 고려합니다.

// 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.


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.

참고URL : https://stackoverflow.com/questions/7189849/socket-io-issue-with-control-chars

반응형