如何解决我敢肯定它很简单,但是在Socket.io聊天中我需要帮助
相信我,我已经搜索过铁杆了,但是我发现并尝试的所有方法均无效。 我想用URL呼叫我的聊天:https:// mychatserver:444 / index.html?username = Chatuser 并希望将此用户名与该用户的消息一起打印在index2.html中 聊天正在进行,但我的用户名仍然有问题
到目前为止,这是我的代码: index.js:
var fs = require('fs');
var options = {
key: fs.readFileSync('pathtokey','utf8'),cert: fs.readFileSync('pathtocert','utf8')
};
var app = require('express')();
var express = require('express');
// Hier teilen wir express mit,dass die öffentlichen HTML-Dateien
// im Ordner "public" zu finden sind.
app.use(express.static(__dirname + '/public'));
var https = require('https').createServer(options,app);
var io = require('socket.io')(https);
app.get('/',(req,res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection',(socket) => {
socket.on('chat message',(msg) => {
console.log('Nachricht ' + msg);
io.emit('chat message',msg);
});
});
https.listen(444,() => {
console.log('listening on *:444');
});
这是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: #fff;
padding: 3px;
position: absolute !important;
top: 0 !important;
width: 100%;
}
form input {
border: 0;
padding: 10px;
width: 79%;
margin-right: 1%;
}
form button {
width: 20%;
background: #137bc0;
color: #fff;
border: none;
padding: 10px;
}
#messages {
list-style-type: none;
margin-top: 50px;
padding: 0;
}
#messages li {
padding: 5px 10px;
}
#messages li:nth-child(odd) {
background: #eee;
}
input#m {
border-bottom: solid 2px #fff;
}
input#m:focus {
outline: none;
border-color: #137bc0;
}
</style>
</head>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function (e) {
e.preventDefault(); // prevents page reloading
socket.emit('chat message','UserName ' + $('#m').val());
$('#m').val('');
return false;
});
});
</script>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" placeholder="Bitte schreiben Sie uns hier Ihre Fragen / Vorschläge"
autocomplete="off" /><button>Senden</button>
</form>
</body>
</html>
这里是index2.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: 0.5%; }
form button { width: 9%; background: rgb(130,224,255); border: none; padding: 10px; }
#messages { display: flex; flex-direction: column-reverse; list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.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>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Senden</button>
</form>
</body>
</html>
....希望您的一位专业人士能够帮助我。 最佳希腊人
解决方法
从请求参数中获取用户名,并将其记住在变量中。
let username = "";
app.get('/',(req,res) => {
username = req.params.username
res.sendFile(__dirname + '/index.html');
});
接下来在socket.io处理程序中使用记住的用户名
io.on('connection',(socket) => {
socket.on('chat message',(msg) => {
io.emit('chat message',username + " " + msg );
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。