在Windows 7上安装node.js的socket io

当我运行节点服务器时,控制台显示“ info – socket.io started ”。 但是,它从不检测来自客户端的连接。 我相信这是安装socket.io的问题,因为我正在运行Windows 7机器。

当我尝试从客户端连接到服务器时,我的服务器控制台显示“ debug-served static content /lib/socket.io.js ”。

任何人有任何build议如何让我的套接字IO成功地从客户端连接到我的Windows 7机器上?

现在,我有位于以下目录中的socket.io文件夹:

的NodeJS / node_modules / socket.io /

我在服务器端节点中的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> 

谢谢!

您的socketio js应位于以下path中

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

在你的情况下,你需要包括如下

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