我正在学习节点和套接字,并遇到了绊脚石。我的服务器运行正常,可以连接和断开用户连接,并将其名称添加到套接字。我拥有它,以便当用户发送消息时,其他登录的人都可以接收到它,并显示在弹出警报中。我需要发送的不仅是用户名之类的消息,而且还可能发送更多数据。我知道json有可能,但是如何在发送和接收的数据中包含它呢?我已包含以下代码。所以我想在发送和接收数据时添加用户名和数据,并在弹出框中显示它,例如“用户名发送了此消息.....”。它在console.log上工作
server.js
var http = require('http');
var Static = require('node-static');
var app = http.createServer(handler);
var io = require('socket.io').listen(app);
var port = 8080;
var files = new Static.Server('./public');
function handler (request, response) {
request.on('end', function() {
files.serve(request, response);
}).resume();
}
var numUsers = 0;
io.on('connection', (socket) => {
var addedUser = false;
socket.on('add user', (username) => {
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
//addedUser = true;
++numUsers;
// echo globally (all clients) that a person has connected
//socket.broadcast.emit('user joined', {
// username: socket.username,
// numUsers: numUsers
console.log(username+' has logged on, now there are '+numUsers+' online');
});
//when receive new coors
socket.on('new_message', function (data) {
socket.broadcast.emit('new_message', data);
console.log(socket.username+' just sent '+data);
});
// when the user disconnects.. perform this
socket.on('disconnect', () => {
--numUsers;
console.log(socket.username+' has left, now there are '+numUsers+' online');
});
});
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of Getting Current Position</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script> var username = prompt('What\'s your username?'); </script>
<script type="text/javascript">
socket = io.connect('/');
socket.emit('add user', username);
function sendmessage() {
var new_message = document.getElementById("new_message").value;
//alert (new_message);
socket.emit('new_message', new_message);
}
socket.on('new_message', function (data) {
//socket.broadcast.emit('new_message', data);
//console.log(data);
alert(data);
});
</script>
</head>
<body>
<button type="button" onclick="sendmessage()">Send message</button><BR><BR>
<input name="new_message" type="text" id="new_message" value=""><BR>
</body>
</html>
答案 0 :(得分:1)
经过大量阅读后,我做了以下更改:
index.html
function sendmessage() {
var new_message = document.getElementById("new_message").value;
var Details3={
username : username,
message : new_message
};
socket.emit('new_message', Details3);
server.js
socket.on('new_message', (data) => {
var New_Details={
username : data.username,
message : data.message
};
socket.broadcast.emit('update_message', New_Details);
console.log(data.username + ' just wrote ' + data.message);
});
答案 1 :(得分:0)
来不及回答,但是您可以在没有json的情况下完成
index.html
function sendmessage() {
socket.emit('new_message', username , new_message);
}
server.js
socket.on('new_message', (username , message) => {
socket.broadcast.emit('update_message', username , message);
console.log(username + ' just wrote ' + message);
});
我希望对您有帮助