如何在wamp上运行套接字以及为localhost使用哪个端口

我在我的项目中第一次使用node.js ,我正在WAMP上运行我的项目。

我创build了app.js和我的app.js的代码是:

var http = require("http"); var url = require("url"); var qs = require("querystring"); // Create an HTTP server for *socket.io* to listen on var app = http.createServer(); var io = require("socket.io").listen(app);app.listen(8080); var authorisedIPs = [ '127.0.0.1', '192.168.0.204' ]; var clients = {}; function handler(req, res){ var remoteAddress = req.socket.remoteAddress; if(authorisedIPs.indexOf(remoteAddress) >= 0) { try{ if(req.method == 'GET'){ var body = ''; req.on('error',function(){ res.writeHead(500, {"Content-Type": "text/plain"}); res.end("Error"); }); req.on('data',function(data){ body += data; if(body.length > 1e6){ response.writeHead(413, {'Content-Type': 'text/plain'}); req.connection.destroy(); } }); req.on('end',function(){ var returned = JSON.parse(body); var client_name = returned.admin_id+'_'+returned.user_id+'_'+returned.login_id; var channel = returned.channel; var event = returned.status; for(var keys in clients){ if(keys == client_name){ var socket_to_send = clients[keys]; socket_to_send.emit(channel,body); } } if(typeof socket_to_send != 'undefined'){ } }); } res.writeHead(200, {"Content-Type": "text/plain"}); res.end("ok"); } catch(error){ res.writeHead(500, {"Content-Type": "text/plain"}); res.end("Error"); } } else{ res.writeHead(401, {"Content-Type": "text/plain"}); res.end("Unauthorised"); } } function sendData(socket){ var thisRef = this; var currentTimeObj = new Date(); var formattedTime = currentTimeObj.getDate() + "-" +currentTimeObj.getMonth() + "-" + currentTimeObj.getFullYear() + " " + currentTimeObj.getHours() + ":" + currentTimeObj.getMinutes() + ":" + currentTimeObj.getSeconds(); socket.emit('timeUpdate', { currentTime: formattedTime}); setTimeout(function(){ sendData.call(thisRef,socket) },1000); } function testfunc(socket){ socket.emit('testEvent', { message: 'testing...'}); } function testfunc1(socket){ socket.emit('testEvent', { message: 'testing1...'}); } io.sockets.on('connection', function (socket) { socket.emit('get_name', {}); socket.on('forceDisconnect',function(data12){ for(var keysd in clients){ if (keysd == data12.my_name) { delete clients[keysd]; socket.disconnect(); } } }); socket.on('take_name', function (data11) { clients[data11.my_name] = socket; }); }); function getsplitText(string,splitter,index){ return_arr = string.split(splitter); return return_arr[index]; } http.createServer(handler).listen(8080, '192.168.0.204'); 

和我的客户端的HTML是:

 <!DOCTYPE html> <html> <head> <title>Server Time poller</title> <meta charset="UTF-8"> </head> <body> <div id="statusMessageDiv"> </div> <div id="serverTimeDiv"></div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script> <script src="192.168.0.204:3000/socket.io/socket.io.js"></script> <script> $(document).ready(function(){ alert('hello'); var socket = io.connect('http://192.168.0.204:8080'); socket.on('testEvent',function(data){ $("#statusMessageDiv").html(data.welcomeMessage); socket.emit('testRevert',{message:'acknowledged'}); }); socket.on('timeUpdate', function (data) { $("#serverTimeDiv").html(data.currentTime); }); }); </script> </html> 

当我在控制台上运行app.js时,我得到了回应

 info - socket.io started. 

但是,当我在浏览器中打开index.html时,我收到警报“hello”,然后出现错误

 ReferenceError: io is not defined var socket = io.connect('http://192.168.0.204:8080'); 

请帮忙。

       

网上收集的解决方案 "如何在wamp上运行套接字以及为localhost使用哪个端口"