gpt4 book ai didi

javascript - 在Windows 7上为node.js安装socket io

转载 作者:太空宇宙 更新时间:2023-11-04 01:15:41 25 4
gpt4 key购买 nike

当我运行 Node 服务器时,控制台显示“info - socket.io started”。但是,它永远不会检测到来自客户端的连接。我认为这是安装 socket.io 的问题,因为我运行的是 Windows 7 机器...

当我尝试从客户端连接到服务器时,我的服务器控制台指示“调试 - 提供静态内容/lib/socket.io.js”。

有人对如何让套接字 io 在我的 Windows 7 计算机上从客户端成功连接有任何建议吗?

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

nodejs/node_modules/socket.io/

对于 Node 中的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应该位于以下路径内

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

根据您的情况,您需要包含以下内容

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

关于javascript - 在Windows 7上为node.js安装socket io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9431032/

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