forked from absmach/supermq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
40 lines (34 loc) · 885 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package ws
import (
"github.com/gorilla/websocket"
"github.com/mainflux/mainflux/pkg/messaging"
)
// Client handles messaging and websocket connection.
type Client struct {
conn *websocket.Conn
id string
}
// NewClient returns a new websocket client.
func NewClient(c *websocket.Conn) *Client {
return &Client{
conn: c,
id: "",
}
}
// Cancel handles the websocket connection after unsubscribing.
func (c *Client) Cancel() error {
if c.conn == nil {
return nil
}
return c.conn.Close()
}
// Handle handles the sending and receiving of messages via the broker.
func (c *Client) Handle(msg *messaging.Message) error {
// To prevent publisher from receiving its own published message
if msg.GetPublisher() == c.id {
return nil
}
return c.conn.WriteMessage(websocket.TextMessage, msg.Payload)
}