我正在使用Socket.io IM服务器,并尝试添加用户名。我已经能够获取用户名并将其保存到名为“ user”的变量中,但是无法将用户名成功附加到该用户的外发消息中。我正在寻找一种无需大幅更改代码即可添加此功能的方法,因为我对socket.io缺乏经验。
我尝试了一些相当愚蠢的事情,例如仅将user变量添加到消息字符串中,但这只会使每个人看起来都像同一个用户-系统会将user的输入值附加到ALL消息中。现在,该网页提示您输入用户名,它只是不执行任何操作,因为我不知道如何分配特定的用户名。
这是我的客户端代码:
var audio = new Audio('https://cdn.glitch.com/a359ff0b-1273-40ab-a005-10968e106f1d%2Falert.wav?1558032194306');
user = prompt("enter username","");
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message' + user, $('#m').val())
$('#m').val('');
return false;
});
//When the server emits " new user connected", add a message which says so.
socket.on('new user', function(msg){
$('#messages').append($('<li>').text('[SYSTEM]: New User Joined'));
audio.play();
});
//When the server emits "user disconnected", add a message which says so.
socket.on('user disconnected', function(msg){
$('#messages').append($('<li>').text('[SYSTEM]: User Left'));
audio.play();
});
socket.on('chat message', function(msg){
var form = $('#m').val();
//logic for commands
var refresh = /#refresh/;
var help = /#help/;
var kill = /#kill/;
var admin = /SUDO/;
var one = refresh.test(msg);
var two = help.test(msg);
var three = kill.test(msg);
var four = admin.test(msg);
if (one == true) {
location = location;
} if (two == true) {
$('#messages').append($('<li>').text('[SYSTEM]: The "#help" command has been activated. The following is an automated response and the user message which activated #help will appear below it in the chat stream. '));
$('#messages').append($('<li>').text("[#HELP]: This message is a brief overview of how to use Megaphone: type a message into the bar above, and hit enter to send it. Refresh the window to clear messages, and to leave, simply close the tab."));
function delay(ms, cb) { setTimeout(cb, ms) }
document.title = "New Message!";
audio.play();
delay(5000, function() {
document.title = "Megaphone";
})
} if (three == true && four == true) {
location = "https://google.com"
} if (three == true && four == false) {
$('#messages').append($('<li>').text(' [SYSTEM]: ATTEMPTED ACCESS TO AN ADMIN COMMAND DETECTED. THE COMMAND CANNOT BE RUN WITHOUT AN ADMIN PASSPHRASE.'));
function delay(ms, cb) { setTimeout(cb, ms) }
document.title = "New Message!";
audio.play();
delay(5000, function() {
document.title = "Megaphone";
})
} if (four == true) {
var success = msg.split('SUDO')[1]
$('#messages').append($('<li>').text(' [ADMIN]: ' + success));
function delay(ms, cb) { setTimeout(cb, ms) }
document.title = "New Message!";
audio.play();
delay(3000, function() {
document.title = "Megaphone";
})
} else {
$('#messages').append($('<li>').text(' [USER]: ' + msg));
function delay(ms, cb) { setTimeout(cb, ms) }
document.title = "New Message!";
audio.play();
delay(5000, function() {
document.title = "Megaphone";
})
};
});
});
这是服务器端:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/about', function(req, res){
res.sendFile(__dirname + '/about.html');
});
http.listen(3000, function(){
console.log('listening on *:3000. Megaphone is functional.');
});
io.on('connection', function(socket) {
console.log('a user connected succesfully.');
io.emit('new user');
socket.on('disconnect', function(){
console.log('a user disconnected succesfully.');
io.emit('user disconnected');
});
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
console.log('message: ' + msg);
});
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});