github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/stream/client.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package stream
    20  
    21  import (
    22  	"encoding/json"
    23  	"sync"
    24  
    25  	m "github.com/e154/smart-home/models"
    26  	"github.com/gorilla/websocket"
    27  )
    28  
    29  // Client ...
    30  type Client struct {
    31  	closed    bool
    32  	user      *m.User
    33  	sessionId string
    34  	*sync.Mutex
    35  	ws *websocket.Conn
    36  }
    37  
    38  // NewClient ...
    39  func NewClient(ws *websocket.Conn, user *m.User, sessionId string) *Client {
    40  	return &Client{
    41  		ws:        ws,
    42  		user:      user,
    43  		Mutex:     &sync.Mutex{},
    44  		sessionId: sessionId,
    45  	}
    46  }
    47  
    48  // WritePump ...
    49  func (c *Client) WritePump(f func(*Client, string, string, []byte)) {
    50  
    51  	var data []byte
    52  	var messageType int
    53  	var err error
    54  	for {
    55  		messageType, data, err = c.ws.ReadMessage()
    56  		if messageType == -1 || err != nil {
    57  			return
    58  		}
    59  
    60  		msg := &Message{}
    61  		_ = json.Unmarshal(data, msg)
    62  		f(c, msg.Id, msg.Query, msg.Body)
    63  	}
    64  }
    65  
    66  // Close ...
    67  func (c *Client) Close() {
    68  	c.closed = true
    69  	if c.ws != nil {
    70  		_ = c.ws.WriteMessage(websocket.CloseMessage, []byte{})
    71  		_ = c.ws.Close()
    72  	}
    73  }
    74  
    75  // Send ...
    76  func (c *Client) Send(id, query string, body []byte) (err error) {
    77  	c.Lock()
    78  	defer c.Unlock()
    79  
    80  	if c.closed {
    81  		return
    82  	}
    83  
    84  	err = c.ws.WriteJSON(&Message{
    85  		Id:    id,
    86  		Query: query,
    87  		Body:  body,
    88  	})
    89  	if err != nil {
    90  		c.closed = true
    91  	}
    92  	return
    93  }
    94  
    95  func (c *Client) GetUser() *m.User {
    96  	return c.user
    97  }
    98  
    99  func (c *Client) SessionID() string {
   100  	return c.sessionId
   101  }