정보를 보내고 받기 위해 웹 소켓을 사용하는 간단한 클라이언트 서버 웹 앱이 있습니다. 클라이언트는 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');});
/////////////////////////////////////////////////////////////////////////////////
고객의 입장은 무엇입니까? 코드는 어때? – max
클라이언트 쪽을 추가 – tony590