2012-04-21 4 views
0

여기 내 재치가 있으므로 도움을 주시면 감사하겠습니다.sockets.io를 사용하여 클라이언트에서 NodeJS 서버로 보내거나 내보낼 수 없습니다.

socket.io를 통해 내 클라이언트 (웹 페이지)에서 내 Node.JS 서버로 메시지를 보내거나 보내려고합니다. 이것은 Windows 7에서 localhost로 실행 중입니다.

정확한 문제는 : 내가 잘 서버에서 클라이언트로 메시지를 보낼 수 있습니다

  • . 클라이언트에서 서버로 메시지를 보내기
  • 작동하지 않습니다 :(

서버 코드 :

<script src="/socket.io/socket.io.js"></script> 

...snip... 

var socket = io.connect('http://localhost'); 
socket.emit('message', {txt:"test"}); 

socket.on('text_msg', function (data) { 
    alert(data.msg); 
}); 

I :

// create http server 
var app = require('http').createServer(httpHandler); 
// socket.io 
var io = require('socket.io').listen(app); 

var port = 8080; 

app.listen(port); 
console.log('Server running at http://127.0.0.1:' + port); 

// socket connection handler 
io.sockets.on('connection', function (client) { 
    // this works great:  
    client.emit('text_msg', {msg: 'Welcome you are now connected.'}); 

    // plain message - this never works :(
    io.sockets.on('message', function(data){ 
     console.log("*************************** message : " + data.txt); 
    }); 
}); 

가 여기

는 클라이언트 코드의 일부입니다 다음 브라우저를 사용해 보았습니다.

  • 크롬 18 (윈도우 7)
  • 파이어 폭스 11 (윈도우 7)
  • 인터넷 익스플로러 9 (윈도우 7) NodeJS 출력에서 ​​

, 내가 크롬과 파이어 폭스 모두 WebSocket을 사용을 참조 수 있으며, IE9는 "사용 htmlfile ". 어떤 도움을

├── [email protected] 
└─┬ [email protected] 
    ├── [email protected] 
    ├── [email protected] 
    └─┬ [email protected] 
    ├─┬ [email protected] 
    │ └── [email protected] 
    ├── [email protected] 
    ├─┬ [email protected] 
    │ ├── [email protected] 
    │ └── [email protected] 
    └── [email protected] 

감사 :

NodeJS은 0.6.15

NPM 목록과 같은 버전입니다.

+0

클라이언트 측에서부터 서버에서 메시지를 수신하면 알림 상자에 표시됩니다. – JimFing

답변

2

변경 :

io.sockets.on('message', function(data){ 
console.log("*************************** message : " + data.txt); 
}); 

사람 :

client.on('message', function(data){ 
console.log("*************************** message : " + data.txt); 
}); 

당신이 소켓의 현재 네임 스페이스입니다 "클라이언트"당신이 정의하는

io.sockets.on('connection', function (client) 

를 호출 할 때 그쪽으로 클라이언트 쪽에서 방출됩니다.

+0

고맙습니다! 나는 그것을 발견하지 못했다고 믿을 수 없다. – JimFing

관련 문제