我使用node& amp; socket.io。它有服务器和客户端代码。
chatApp.js
"use strict";
let app = require('express')();
let server = require('http').Server(app);
let io = require('socket.io')(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + "/logs/chat2.html");
});
io.on("connect", (socket) => {
// New user
socket.emit("userSelfNotification", {text: "--SELF-- You joined server"});
socket.broadcast.emit("newUserNotification", {text: "--BROADCAST-- New user"});
// Send sms
socket.on("message", (message) => {
io.sockets.emit("newMessage", {text: message});
});
// Change room
socket.on("changeRoom", (room) => {
socket.room = room;
socket.broadcast.emit("leaveRoom", {text: "--BROADCAST-- user left "});
socket.join(room);
socket.in(room).emit("roomChangeNotification", {text: "--ROOM-- You left"});
io.sockets.in(room).emit("newUserNotification", {text: "--ROOM_ALL-- New user joined"});
});
});
server.listen(3000);
client.html
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Чат</title>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" charset="utf-8"></script>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
let socket = io('http://localhost:3000/');
socket.on("userSelfNotification", (message) => {
$("textarea").val($("textarea").val() + message.text + "\n");
});
socket.on("newUserNotification", (message) => {
$("textarea").val($("textarea").val() + message.text + "\n");
});
socket.on("newMessage", (message) => {
$("textarea").val($("textarea").val() + message.text + "\n");
});
socket.on("leaveRoom", (message) => {
$("textarea").val($("textarea").val() + message.text + "\n");
});
socket.on("roomChangeNotification", (message) => {
$("textarea").val($("textarea").val() + message.text + "\n");
});
socket.on("newUserNotification", (message) => {
$("textarea").val($("textarea").val() + message.text + "\n");
});
</script>
</head>
<body>
<textarea name="name" rows="15" cols="50"></textarea>
<p></p>
<input type="text" name="text" size="20">
<button type="button" name="button">Отправить</button>
<button type="button" name="changeRoomButton">Сменить комнату</button>
<script>
$(document).on('click', 'button', () => {
let message = $('input').val();
socket.emit("message", message);
$('input').val(null);
});
$(document).ready(function(){
$("[name='changeRoomButton']").click(function(){
$(".test").hide();
console.log("I work babeee");
socket.emit("changeRoom", "room");
});
});
</script>
</body>
</html>
主要问题是我无法实现房间更改:即使用户更改了房间,他/她仍然可以写入其他默认房间并从中读取其他消息。此外,两个房间都会发出一些事件。
帮助我了解我的错在哪里,谢谢!
答案 0 :(得分:0)
由于套接字客户端可以订阅多个房间,如果您只想让您的客户一次只能在一个房间内,那么您必须在当时只能进行.leave
加入新的。
访问客户订阅的roomNames的实际方法是使用:socket.rooms
但是,由于您没有订阅(加入)/取消订阅(离开)到多个频道(会议室)并且一次只有一个活动房间,您可以致电:socket.leaveAll,然后执行socket.join(room)
例如,如果要在客户端断开连接时通知您必须在disconnecting
上执行此操作,因为在disconnect
上已经调用了leaveAll():
socket.on('disconnect', function(){(
/*
socket.rooms is empty here
leaveAll() has already been called
*/
});
socket.on('disconnecting', function(){
// socket.rooms should isn't empty here
var rooms = socket.rooms.slice();
/*
here you can iterate over the rooms and emit to each
of those rooms where the disconnecting user was.
*/
});
答案 1 :(得分:0)
感谢您的回答:每一个似乎都是半正确的,但过于抽象。我通过将“socket.room”重新分配到我希望加入的房间来解决这个难题。这个块解决了这个问题:
// Change room
socket.on("changeRoom", (room) => {
socket.in(defaultRoom).broadcast.emit("leaveRoom", {text: "--BROADCAST-- User left room"});
socket.leave(defaultRoom);
socket.room = room;
socket.join(room);
socket.emit("roomChangeNotification", {text: "--ROOM-- You changed room"});
socket.in(room).broadcast.emit("OnewUserNotification", {text: "--ROOM_ALL-- New user joined"});
});
SO: 1.我添加了默认房间并自动加入了连接到服务器的每个用户 2.我添加了将socket.room参数从“defaultRoom”重新指定到名为“room”的自定义房间的代码。就是这样。