1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00
Mainflux.mainflux/ws/client.go
b1ackd0t ada5813f47
MF-1455 - Update Versions of Protobuf (#1704)
* initial commit
* add protoc-gen-gofast
* update generated files
* fix linting
* fix consumers error on message conversion
* fix copying values on transformers
* initial commit
* initial commit
* add protoc-gen-gofast
* update generated files
* fix linting
* fix consumers error on message conversion
* fix copying values on transformers
* embedded for forward compatible.
* remove gogo
* embedded for forward compatible.
* update protoc compiler
* fix linting
* remove hex comment

Signed-off-by: rodneyosodo <socials@rodneyosodo.com>
2023-02-02 18:28:32 +01:00

41 lines
878 B
Go

// 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 Client object
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)
}