I started to understand web-apps, and try to write a small chat. I don’t know much about the node, to work with the base on the backend of the php framework, all the ajax requests to its controllers.
On the client side, I initialize the connection:
this.webSocket = new WebSocket('ws://localhost:8084'); Accordingly, I hang up the handler:
this.webSocket.send(message); The node is responsible for the server part (separate script file):
var WSServer = new WS.Server({port: 8084}); WSServer.on('connection', function (ws) { //Подписываемся на событие - получение сообщения ws.on('message', function (text) { console.log('Сообщение получено. Текст: ' + text); At this stage, everything is clear. Connect users in the browser, even the correspondence are.
But how to identify the connected user in the server file? Now it turns out that they all write to one channel and their messages are not logically separated.
Let's say if the id of the group of the user who connected in the browser is 123 then all messages should be saved to the group with 123. This is exactly how I imagine the mechanism, if the principle is different, tell me how it is correct?
To send data, use the socket.send (data) method. You can send any data.
It is written in the manual https://learn.javascript.ru/websockets#primer-brauzernogo-koda
When you try to transfer to
websocket.send({ userid : 1, group : 2, message: 3, }) in the log on the server I get the Сообщение получено. Текст: [object Object] Сообщение получено. Текст: [object Object] I cannot call on my keys either.
How to do it and what I have not learned in this issue?
JSON.stringify- nörbörnën 5:14