2014-01-17 2 views
0

정보를 보내고 받기 위해 웹 소켓을 사용하는 간단한 클라이언트 서버 웹 앱이 있습니다. 클라이언트는 config 파일을 제대로 연결하고 수신 할 수 있지만 "socket.emit ('message', {my : 'data'});"를 사용하여 클라이언트에서 "test"메시지를 보내려고하면 표시되지 않습니다 . 서버에 나는 Wireshark를 확인 않았고 패킷이 서버에 도착하는socket.io가 데이터를 수신하지 않습니다.

var sIoPort = 8181; 
var host = '192.168.4.111'; 
var fs = require('fs'); 

var iniMsg = fs.readFileSync('data.json','utf8'); 

var http = require("http").createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end(index); 
}); 
http.listen(sIoPort,host); 

var browserServer = require('socket.io').listen(http); 
browserServer.on('connection', function (socket) { 
    console.log('Client websocket connected'); 

    // send the config file if available 
    browserServer.sockets.emit('msg',iniMsg.toString()); 
}); 

browserServer.on('message', function (message) { 
    console.log('received message: ' + message); 
}); 

클라이언트 측

/////////////////////////////////////////////////////////////////////////////// 
socket = io.connect("192.168.4.111",{"port":8181}); 
socket.on('connect',function() {if(DEBUG) console.log('Socket Connected');}); 
socket.emit('message', {my: 'data'}); // test if server receives message 
socket.on('msg',function(data) { 
    var json = JSON.parse(data); 

    // add the maps to the the GUI 
    switch(json.type) { 
     case 'maps': add_maps_from_json(json, null); 
        break; 
    } 
}); 

socket.on('disconnect',function() {if(DEBUG) console.log('Socket Disconnected');}); 
///////////////////////////////////////////////////////////////////////////////// 
+0

고객의 입장은 무엇입니까? 코드는 어때? – max

+0

클라이언트 쪽을 추가 – tony590

답변

1

이 소켓의 이벤트에주의를 지불하는 것 때문에 서버 측 청취자 수정합니다.

browserServer.on('connection', function (socket) { 
    console.log('Client websocket connected'); 

    // send the config file if available 
    browserServer.sockets.emit('msg',iniMsg.toString()); 

    socket.on('message', function (message) { 
    console.log('received message: ' + message); 
    }); 

}); 
+0

Thx 메이트, 어리석은 실수를 어떻게 저지를 수 있습니까? 고맙습니다 – tony590

관련 문제