我在项目中第一次使用 node.js ,我在WAMP上运行我的项目。
我创建了 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 时,我会收到警告&#39;你好&#39;然后错误
ReferenceError: io is not defined
var socket = io.connect('http://192.168.0.204:8080');
请帮忙。
答案 0 :(得分:0)
正确审核后,我认为问题出在您的端口上:
您在此处使用端口3000:
<script src="192.168.0.204:3000/socket.io/socket.io.js"></script>
您的应用在端口8080