Skip to main content

Receive Parsed Web Socket Messages

Last updated on

Use the following function to receive a parsed websocket message from the server:

//messageHandler is callback function how to handle incoming ws message
var messageHandler = func(dataByte []byte) {
message, err := parser.UnmarshalResponse(dataByte)
if err != nil {...}

// print all incoming message type
logrus.Infof("Message type: %v", message.Type())

marshal, err := json.Marshal(message)
if err != nil {...}

// print all incoming message content
logrus.Infof("Message content: %v", string(marshal))

// if you want to specify the message type and write logic for each of it
switch message.Type() {
case model.TypeNotificationMessage:
notificationMsg := message.(model.NotificationMessage)
logrus.Infof("Notification is coming from %s to %s", notificationMsg.From, notificationMsg.To)

case model.TypeKickResponse:
kickMessage := message.(model.KickResponse)
logrus.Infof("Message id is: %s", kickMessage.MessageID)
}
}