如何使用 Socket.io 构建 Node.js WebSocket 聊天应用程序?
How to build a Node.js WebSocket Chat App with Socket.io?
我正在尝试使用 socket.io 构建一个聊天应用程序。我的所有代码都与文档相同。但是我的代码不工作,我找不到问题所在。我哪里出错了?
index.html:
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(e){
e.preventDefault(); // prevents page reloading
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($'<li>').text(msg);
});
});
</script>
</body>
</html>
index.js:
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
console.log('listening on *:3000');
});
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
console.log('message: '+ msg);
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
});
package.json:
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.17.1",
"socket.io": "^2.2.0"
}
}
根据这些代码,当我向服务器发送消息时希望在其他客户端看到我的消息,但甚至不会出现在发件人中。
删除嵌套在第一个中的第二个 socket.on("chat message")
。
感谢您的帮助。我发现了问题,这是$('#messages').append($'<li>').text(msg);
行html文件中js代码的语法错误。应该是 $('#messages').append($('<li>').text(msg));
我正在尝试使用 socket.io 构建一个聊天应用程序。我的所有代码都与文档相同。但是我的代码不工作,我找不到问题所在。我哪里出错了?
index.html:
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(e){
e.preventDefault(); // prevents page reloading
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($'<li>').text(msg);
});
});
</script>
</body>
</html>
index.js:
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
console.log('listening on *:3000');
});
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
console.log('message: '+ msg);
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
});
package.json:
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.17.1",
"socket.io": "^2.2.0"
}
}
根据这些代码,当我向服务器发送消息时希望在其他客户端看到我的消息,但甚至不会出现在发件人中。
删除嵌套在第一个中的第二个 socket.on("chat message")
。
感谢您的帮助。我发现了问题,这是$('#messages').append($'<li>').text(msg);
行html文件中js代码的语法错误。应该是 $('#messages').append($('<li>').text(msg));