저는 node.js와 socket.io를 처음 사용하고 서버를 클라이언트에 연결하려고 시도했습니다 (예제 : http://socket.io/#how-to-use). (더 로컬 호스트 없음)Node.js + socket.io : 서버의 앱이 제대로 작동하지 않습니다.
가 서버 :
이var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html'+err);
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.on('message', function(msg){
console.log('Got text: '+msg);
socket.broadcast.send(msg);
});
socket.on('disconnect', function() { });
});
클라이언트 :
<html><head><script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on('connect', function() {
alert('connected.');
socket.on('message', function (msg) {
// my msg
alert('message received: '+msg);
});
socket.send('hi');
});
</script>
</head><body>This is the content :)</body>
</html>
가 콘솔에서
구글 크롬 표시 : 또한
Unexpected response code: 502
, 모든 메시지를받은 후, 크롬
추가GET http://[myServer]/socket.io/1/?t=1352313105809 socket.io.js:1659
Socket.handshake socket.io.js:1659
Socket.connect socket.io.js:1699
maybeReconnect
콘솔. Wheres 문제가 있습니까?
감사합니다. 코드를 편집하고 연결 했으므로 스크립트 (socket.io.js)가로드 된 것 같습니다. 그러나 아무런 메시지도받지 못했습니다. 메시지가 실제로 전송되는지 테스트 할 수있는 방법이 있습니까? – xoxox
@ xoxox 내 답변을 업데이트했습니다. – mabako
감사합니다. 마침내 자체 메시지를 받지만 다른 메시지는 수신하지 않습니다 (다른 브라우저로 연결할 때). Opera는 콘솔에 아무것도 인쇄하지 않습니다. Google 크롬은 "예기치 않은 응답 코드 : 502"를 인쇄하지만 여전히 작동합니다. (질문을 편집하고 정보를 추가했습니다.) – xoxox