Я новичок с реакцией. И я хочу настроить групповой чат с помощью реакции и socket.io. Но есть некоторые трудности, когда я пытаюсь это сделать. Ниже приведен пример моего кода. Любая помощь будет оценена.Интеграция не сработала и Socketio
Server.js
const io = require('socket.io')(http)
// Listen for a connection
// usernames which are currently connected to the chat
var usernames = {};
io.sockets.on('connection', function (socket) {
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function (room) {
// store the room name in the socket session for this client
socket.room = 'room1';
// send client to room 1
socket.join('room1');
console.log("join room1 " + room)
});
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
console.log("message " + JSON.stringify(data))
// console.log(socket);
socket.broadcast.in('room1').emit('updatechat', data);
});
socket.on('disconnect', function(){
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
socket.leave(socket.room);
});
})
Client.js
let name = "test"
socket.emit('adduser', name);
socket.emit("sendchat", message_text)
socket.on('updatechat', function (data) {
messages.push(data);
console.log("Hello!!!");
});
То, что я хочу сделать, это добавить текстовое сообщение в чате борту. Я пробовал трансляцию, как в коде, и добавляю ее как состояние в компонент реакции. Тем не менее, он может только добавить сообщение с этим определенным пользователем. Что случилось с моим кодом?
В коде клиента у вас есть 'socket.emit (" sendchat ", message_text)' без ';' –