2020-05-24 03:57:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2020-06-18 09:01:49 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-05-24 03:57:49 +03:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
const channelBufSize = 100
|
|
|
|
|
|
|
|
// Chat client.
|
|
|
|
type Client struct {
|
2020-06-11 23:33:20 +03:00
|
|
|
id string
|
2020-05-24 03:57:49 +03:00
|
|
|
ws *websocket.Conn
|
|
|
|
server *Server
|
2020-06-15 02:44:38 +03:00
|
|
|
ch chan *ChatMessage
|
|
|
|
pingch chan *PingMessage
|
|
|
|
|
2020-05-24 03:57:49 +03:00
|
|
|
doneCh chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new chat client.
|
|
|
|
func NewClient(ws *websocket.Conn, server *Server) *Client {
|
|
|
|
|
|
|
|
if ws == nil {
|
2020-06-18 09:01:49 +03:00
|
|
|
log.Panicln("ws cannot be nil")
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if server == nil {
|
2020-06-18 09:01:49 +03:00
|
|
|
log.Panicln("server cannot be nil")
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-06-15 02:44:38 +03:00
|
|
|
ch := make(chan *ChatMessage, channelBufSize)
|
2020-05-24 03:57:49 +03:00
|
|
|
doneCh := make(chan bool)
|
2020-06-15 02:44:38 +03:00
|
|
|
pingch := make(chan *PingMessage)
|
2020-06-11 23:33:20 +03:00
|
|
|
clientID := getClientIDFromRequest(ws.Request())
|
2020-06-15 02:44:38 +03:00
|
|
|
return &Client{clientID, ws, server, ch, pingch, doneCh}
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Conn() *websocket.Conn {
|
|
|
|
return c.ws
|
|
|
|
}
|
|
|
|
|
2020-06-15 02:44:38 +03:00
|
|
|
func (c *Client) Write(msg *ChatMessage) {
|
2020-05-24 03:57:49 +03:00
|
|
|
select {
|
|
|
|
case c.ch <- msg:
|
|
|
|
default:
|
|
|
|
c.server.Del(c)
|
|
|
|
err := fmt.Errorf("client %d is disconnected.", c.id)
|
|
|
|
c.server.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Done() {
|
|
|
|
c.doneCh <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen Write and Read request via chanel
|
|
|
|
func (c *Client) Listen() {
|
|
|
|
go c.listenWrite()
|
|
|
|
c.listenRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen write request via chanel
|
|
|
|
func (c *Client) listenWrite() {
|
|
|
|
for {
|
|
|
|
select {
|
2020-06-15 02:44:38 +03:00
|
|
|
// Send a PING keepalive
|
|
|
|
case msg := <-c.pingch:
|
|
|
|
websocket.JSON.Send(c.ws, msg)
|
2020-05-24 03:57:49 +03:00
|
|
|
// send message to the client
|
|
|
|
case msg := <-c.ch:
|
2020-06-15 02:44:38 +03:00
|
|
|
msg.MessageType = "CHAT"
|
2020-06-17 05:08:34 +03:00
|
|
|
// log.Println("Send:", msg)
|
2020-05-24 03:57:49 +03:00
|
|
|
websocket.JSON.Send(c.ws, msg)
|
|
|
|
|
|
|
|
// receive done request
|
|
|
|
case <-c.doneCh:
|
|
|
|
c.server.Del(c)
|
|
|
|
c.doneCh <- true // for listenRead method
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen read request via chanel
|
|
|
|
func (c *Client) listenRead() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
// receive done request
|
|
|
|
case <-c.doneCh:
|
|
|
|
c.server.Del(c)
|
|
|
|
c.doneCh <- true // for listenWrite method
|
|
|
|
return
|
|
|
|
|
|
|
|
// read data from websocket connection
|
|
|
|
default:
|
2020-06-15 02:44:38 +03:00
|
|
|
var msg ChatMessage
|
2020-05-24 03:57:49 +03:00
|
|
|
err := websocket.JSON.Receive(c.ws, &msg)
|
|
|
|
if err == io.EOF {
|
|
|
|
c.doneCh <- true
|
|
|
|
} else if err != nil {
|
|
|
|
c.server.Err(err)
|
|
|
|
} else {
|
|
|
|
c.server.SendAll(&msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|