socket.io并表示3错误

运行app.js时我没有错误,但是我无法获得用户名的JavaScript提示。 我只能加载chat.html一切都无法正常工作。 我认为这个问题可能是:

io = require('socket.io')。listen(server); 要么

server.listen(80);

同样在networking标签中,我得到:?t = 1394734845750 /socket.io/1 GET(失败)net :: ERR_CONNECTION_REFUSED socket.io.js:1659红色。

app.js

var app = require('express')() , server = require('http').createServer(app) , io = require('socket.io').listen(server); server.listen(80); var fs = require('fs'); // routing app.get('/', function (req, res) { res.sendfile(__dirname + '/chat.html'); }); // usernames which are currently connected to the chat var usernames = {}; function check_key(v) { var val = ''; for(var key in usernames) { if(usernames[key] == v) val = key; } return val; } io.sockets.on('connection', function (socket) { // when the client emits 'sendchat', this listens and executes socket.on('sendchat', function (data) { // we tell the client to execute 'updatechat' with 2 parameters io.sockets.emit('updatechat', socket.username, data); }); // when the client emits 'adduser', this listens and executes socket.on('adduser', function(username){ // we store the username in the socket session for this client socket.username = username; // add the client's username to the global list usernames[username] = socket.id; // echo to client they've connected socket.emit('updatechat', 'SERVER', 'you have connected'); // echo to client their username socket.emit('store_username', username); // echo globally (all clients) that a person has connected socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected: ' + socket.id); // update the list of users in chat, client-side io.sockets.emit('updateusers', usernames); }); // when the user disconnects.. perform this socket.on('disconnect', function(){ // remove the username from global usernames list delete usernames[socket.username]; // update list of users in chat, client-side io.sockets.emit('updateusers', usernames); // echo globally that this client has left socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected'); }); // when the user sends a private msg to a user id, first find the username socket.on('check_user', function(asker, id){ //console.log("SEE: "+asker); console.log(id); io.sockets.socket(usernames[asker]).emit('msg_user_found', check_key(id)); }); // when the user sends a private message to a user.. perform this socket.on('msg_user', function(usr, username, msg) { //console.log("From user: "+username); //console.log("To user: "+usr); //console.log(usernames); io.sockets.socket(usernames[usr]).emit('msg_user_handle', username, msg); fs.writeFile("chat_data.txt", msg, function(err) { if(err) { console.log(err); } /*else { console.log("The file was saved!"); }*/ }); }); }); 

chat.html

 <script src="/socket.io/socket.io.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> var my_username = ''; function send_individual_msg(id) { //alert(id); //alert(my_username); socket.emit('check_user', my_username, id); //socket.emit('msg_user', id, my_username, prompt("Type your message:")); } var socket = io.connect('http://localhost:8008'); // on connection to server, ask for user's name with an anonymous callback socket.on('connect', function(){ // call the server-side function 'adduser' and send one parameter (value of prompt) socket.emit('adduser', prompt("What's your name?")); }); // listener, whenever the server emits 'msg_user_handle', this updates the chat body socket.on('msg_user_handle', function (username, data) { $('#conversation').append('<b>'+username + ':</b> ' + data + '<br>'); }); // listener, whenever the server emits 'msg_user_found' socket.on('msg_user_found', function (username) { //alert(username); socket.emit('msg_user', username, my_username, prompt("Type your message:")); }); // listener, whenever the server emits 'updatechat', this updates the chat body socket.on('updatechat', function (username, data) { $('#conversation').append('<b>'+username + ':</b> ' + data + '<br>'); }); // listener, whenever the server emits 'store_username', this updates the username socket.on('store_username', function (username) { my_username = username; }); // listener, whenever the server emits 'updateusers', this updates the username list socket.on('updateusers', function(data) { //alert(data); //console.log(data); $('#users').empty(); $.each(data, function(key, value) { $('#users').append('<div style="cursor:pointer;" onclick="send_individual_msg(\''+value+'\')">' + key + '</div>'); }); }); // on load of page $(function(){ // when the client clicks SEND $('#datasend').click( function() { var message = $('#data').val(); if(message == '' || jQuery.trim(message).length == 0) return false; $('#data').val(''); // tell server to execute 'sendchat' and send along one parameter socket.emit('sendchat', message); }); // when the client hits ENTER on their keyboard $('#data').keypress(function(e) { if(e.which == 13) { $(this).blur(); //$('#datasend').focus().click(); $('#datasend').click(); } }); }); </script> <div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;"> <b>USERS</b> <div id="users"></div> </div> <div style="float:left;width:550px;height:250px;overflow:scroll-y;padding:10px;"> <div id="conversation"></div> <input id="data" style="width:200px;" /> <input type="button" id="datasend" value="send" /> </div> 

您的服务器正在侦听端口80,但是您的套接字正在连接到端口8008.更改其中一个以使它们位于同一个端口上。