@baris I see. Thank you once again.
M
Mononoke
@Mononoke
Posts
-
could someone please help me how to properly monitor event:chats.receive -
could someone please help me how to properly monitor event:chats.receivesocket.on('connect', () => { console.log('Connected to the socket server'); socket.emit('modules.chats.enter',1); socket.emit('modules.chats.enter',2); });
I have made the above modifications and they have been effective! Thank you very much for your response! I just have a small question. If I, as a user, join many chat rooms, do I also need to iterate and join them all in the socket.connect method?
-
could someone please help me how to properly monitor event:chats.receiveIn a separate JavaScript file, I can monitor the newtopic and newpost events by configuring the code below, but chats.receive is not working. I am a beginner in JavaScript. Can someone tell me how to make it work?
'use strict'; const axios = require('axios'); const FormData = require('form-data'); const io = require('socket.io-client'); const forum = 'https://mynodebb.com/'; const socketUrl = 'wss://mynodebb.com'; async function loginAndGetCookie(url, username, password) { try { let data = new FormData(); data.append('username', username); data.append('password', password); let config = { method: 'post', maxBodyLength: Infinity, url: `${url}api/v3/utilities/login`, headers: { ...data.getHeaders() }, data: data }; const response = await axios.request(config); const cookies = response.headers['set-cookie']; if (!cookies) { throw new Error('No cookies found in the response'); } console.log('Cookies retrieved:', cookies); return cookies.map(cookie => cookie.split(';')[0]).join('; '); } catch (error) { console.error('Error during login:', error); throw error; } } async function getCSRFToken(url, cookies) { try { const response = await axios.get(`${url}api/config`, { headers: { 'Cookie': cookies } }); const csrfToken = response.data.csrf_token; if (!csrfToken) { throw new Error('No CSRF token found in the response'); } console.log('CSRF Token retrieved:', csrfToken); return csrfToken; } catch (error) { console.error('Error getting CSRF token:', error); throw error; } } function connectSocket(url, cookies, csrfToken, roomId, userId) { const socket = io(url, { reconnectionAttempts: 5, reconnectionDelay: 200, path: '/socket.io', transports: [ "polling", "websocket" ], query: { _csrf: csrfToken, //EIO: 4, //transport: 'websocket' }, extraHeaders: { 'Cookie': cookies } }); socket.on('connect', () => { console.log('Connected to the socket server'); }); socket.on('event:new_topic', function (data) { console.log('a new topic was posted', data); }); socket.on('event:chats.receive',function(data){ console.log("a new chats message:",data) }) socket.on('event:new_post', function(data){ console.log('a new post',data) }); socket.on('connect_error', (error) => { console.error('Connection error:', error); }); socket.on('connect_timeout', () => { console.error('Connection timeout'); }); socket.on('error', (error) => { console.error('Socket error:', error); }); } async function main() { const url = forum; const username = 'username'; const password = 'password'; const roomId = '1'; try { const cookies = await loginAndGetCookie(url, username, password); const csrfToken = await getCSRFToken(url, cookies); // Get user ID from login response const response = await axios.get(`${url}api/user/${username}`, { headers: { 'Cookie': cookies } }); const userId = response.data.uid; connectSocket(socketUrl, cookies, csrfToken, roomId, userId); } catch (error) { console.error('Error:', error); } } main();