gpt4 book ai didi

php - 如何在 wamp 上运行套接字以及本地主机使用哪个端口

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:38 24 4
gpt4 key购买 nike

我第一次在我的项目中使用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时,我收到警报“hello”,然后出现错误

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

请帮忙。

最佳答案

经过正确检查后,我认为问题出在您的端口上:

您在此处使用端口 3000: <script src="192.168.0.204:3000/socket.io/socket.io.js"></script>

并且您的应用程序在端口 8080 上运行

关于php - 如何在 wamp 上运行套接字以及本地主机使用哪个端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23357095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com