2012-02-24 5 views
1

노드 서버를 실행할 때 콘솔에 "info-socket.io started"이 표시됩니다. 그러나 클라이언트 측에서 연결을 감지하지 못합니다. 나는 그것이 Windows 7 컴퓨터를 실행하고있어 socket.io 설치 문제라고 생각합니다 ...Windows 7에서 node.js 용 소켓 io 설치

클라이언트 측에서 서버에 연결하려고하면 서버 콘솔에 "debug-served static content /lib/socket.io.js ".

누구나 내 제안에 어떻게 소켓 io 성공적으로 내 Windows 7 컴퓨터에서 클라이언트 쪽에서 연결할 수 있습니까? 지금

, 나는 다음과 같은 디렉토리에있는 socket.io 폴더가 :

nodejs/node_modules/socket.io/

I는 TCP에 대한 서버 측에 다음 코드가 노드에서 서버 :

var http = require('http'), 
    sys = require('util'), 
    fs = require('fs'), 
    io = require('socket.io'); 

console.log('Creating server...'); 
var server = http.createServer(function(request, response) { 
    response.writeHead(200, { 
    'Content-Type': 'text/html' 
    }); 

    var rs = fs.createReadStream(__dirname + '/template.html'); 
    sys.pump(rs, response); 

}); 

var socket = io.listen(server); 

socket.on('connection', function(client) { 

    var username; 

    console.log('New client connected.'); 
    client.send('Welcome to this socket.io chat server!'); 
    client.send('Please input your username: '); 

    client.on('message', function(message) { 
    if (!username) { 
     username = message; 
     client.send('Welcome, ' + username + '!'); 
     return; 
    } 

    socket.broadcast(username + ' sent: ' + message); 
    }); 

}); 

server.listen(20000); 

이것은 클라이언트 측에 코드입니다 :

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <title>Chat</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> 
    <script type="text/javascript" src="http://localhost:20000/socket.io/lib/socket.io.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
     var entry_el = $('#entry'); 
     var socket = new io.Socket('localhost', {port: 20000}); 
     socket.connect(); 
     console.log('connecting...'); 
     socket.on('connect', function() { 
      console.log('connect'); 
     }); 
     socket.on('message', function(message) { 
      var data = message.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"); 
      $('#log ul').append('<li>' + data + '</li>'); 
      window.scrollBy(0, 1000000000000000); 
      entry_el.focus(); 
     }); 

     entry_el.keypress(function(event) { 
      if (event.keyCode != 13) return; 
      var msg = entry_el.attr('value'); 
      if (msg) { 
      socket.send(msg); 
      entry_el.attr('value', ''); 
      } 
     }); 

     }); 
    </script> 
    <style type="text/css"> 
     body { 
     background-color: #666; 
     color: fff; 
     font-size: 14px; 
     margin: 0; 
     padding: 0; 
     font-family: Helvetica, Arial, Sans-Serif; 
     } 
     #log { 
     margin-bottom: 100px; 
     width: 100%; 
     } 
     #log ul { 
     padding: 0; 
     margin: 0; 
     } 
     #log ul li { 
     list-style-type: none; 
     } 
     #console { 
     background-color: black; 
     color: white; 
     border-top:1px solid white; 
     position: fixed; 
     bottom: 0; 
     width: 100%; 
     font-size: 18px; 
     } 
     #console input { 
     width: 100%; 
     background-color: inherit; 
     color: inherit; 
     font-size: inherit; 
     } 
    </style> 
    </head> 
    <body> 
    <h1>Chat</h1> 
    <div id="log"><ul></ul></div> 
    <div id="console"> 
     <input type="text" id="entry" /> 
    </div> 
    </body> 
</html> 

고마워요!

+0

내가 아주 늦게 파티에있어 다음과 같이 포함 할 필요가 귀하의 경우에는 다음과 같은 경로

http://{host:port}/socket.io/socket.io.js 

내부에 있지만, socket.io 클라이언트입니다한다 사이드 js 문제없이로드 된? – luxas

답변

1

귀하의 socketio JS는

<script type="text/javascript" src="http://localhost:20000/socket.io/lib/socket.io.js"></script>