Why does the ws module accept different message events like socket.io?

socket.io:

socket.on("message",function(msg){ })
socket.on("privite",function(msg){})

allows ws to distinguish between different message events like this:

wss.on("connection", function connection(ws,t) {
ws.on("message1", function (message) {})
ws.on("message2", function (message) {})
....

the client uses HTML5 WebSocket, but the htms5 WebSocket event method can only send messages by ws.send (data), so how to define message events

Mar.20,2021

to customize a message protocol with message types, for example:

{
  type: "message_type",
  data: "message_data"
}

is then distributed according to type.

Menu