使用来自socket.io库的websockets在nodejs中编程聊天,我有一个客户端也使用来自socket.io的websockets但我无法让两者协同工作。由于某种原因,客户端不会将消息发送到服务器(我有一个console.log函数,它应该在收到消息时写入控制台,但它不会写任何内容)。
服务器的代码是:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
console.log('message: ' + msg);
io.emit('chat message', msg);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
对于客户来说是:
<!DOCTYPE html>
<html>
<head>
<style>
* { margin:0; padding:0; font-size:11px; font-family:arial; color:#444; }
body { padding:20px; }
#message-list { list-style-type:none; width:300px; height:300px; overflow:auto; border:1px solid #999; padding:20px; }
#message-list li { border-bottom:1px solid #ccc; padding-bottom:2px; margin-bottom:5px; }
code { font-family:courier; background:#eee; padding:2px 4px; }
</style>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script>
// Sends a message to the server via sockets
function sendMessageToServer(message) {
socket.send(message);
log('<span style="color:#888">Sending "' + message + '" to the server!</span>');
}
// Outputs to console and list
function log(message) {
var li = document.createElement('li');
li.innerHTML = message;
document.getElementById('message-list').appendChild(li);
}
// Create a socket instance
socket = new WebSocket('ws://localhost:8080');
// Open the socket
socket.onopen = function(event) {
console.log('Socket opened on client side',event);
// Listen for messages
socket.onmessage = function(event) {
console.log('Client received a message',event);
};
// Listen for socket closes
socket.onclose = function(event) {
console.log('Client notified socket has closed',event);
};
};
</script>
</head>
<body>
<p>Messages will appear below (and in the console).</p><br />
<ul id="message-list"></ul>
<ul style="margin:20px 0 0 20px;">
<li>Type <code>socket.disconnect()</code> to disconnect</li>
<li>Type <code>socket.connect()</code> to reconnect</li>
<li>Type <code>sendMessageToServer('Your Message')</code> to send a message to the server</li>
</ul>
</body>
</html>
你知道这里有什么问题吗? 提前谢谢!
答案 0 :(得分:0)
根据我的知识,你有几个问题。如果它们都是固定的,它应该可以工作。
<script src="http://localhost:3000/socket/socket.io.js"></script>
我不是百分百肯定,因为你的端口可能会有所不同。var socket = io();
。你这样做的方式是socket = new WebSocket('ws://localhost:8080');
未定义WebSocket的地方。并且,代码将创建一个新的websocket服务器,您已在服务器代码中执行此操作。您在客户端需要做的只是连接到服务器。socket.emit('chat message', 'content you want to send'
。 如果所有这四件事都得到了解决,那就可以了。
如果我的表达不清楚,我为你写了一个超级简单的例子:
服务器端:
let io = socketio(server.listener);
io.on("connection", function(socket) {
console.log("A user connected");
// auto messages when a user is connected
setTimeout(function() {
socket.send("message at 4 seconds");
}, 4000);
setTimeout(function() {
socket.send("message at 8 seconds");
}, 8000);
// callback when a user disconnected
socket.on("disconnect", function(){
console.log("A user disconnected");
});
// receive message from client
socket.on("client-server", function(msg) {
console.log(msg);
});
});
// you put this code anywhere when you need to send message from server to client
io.emit("server-client", "server to client message");
客户方:
<!DOCTYPE html>
<html>
<head><title>Hello world</title></head>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('server-client', function(data) {document.write(data)});
socket.emit('client-server', 'test message');
</script>
<body>Hello world</body>
</html>
我的服务器代码是用typescript编写的,所以不要介意let关键字,双引号等等。