139 lines
2.6 KiB
Go
139 lines
2.6 KiB
Go
![]() |
package service
|
|||
|
|
|||
|
import (
|
|||
|
"SciencesServer/app/logic"
|
|||
|
"SciencesServer/serve/logger"
|
|||
|
"encoding/json"
|
|||
|
"github.com/gorilla/websocket"
|
|||
|
"sync"
|
|||
|
"time"
|
|||
|
)
|
|||
|
|
|||
|
type Websocket struct {
|
|||
|
ID string
|
|||
|
conn *websocket.Conn
|
|||
|
send chan []byte
|
|||
|
time *time.Ticker
|
|||
|
}
|
|||
|
|
|||
|
type Hub struct {
|
|||
|
clients map[string]*Websocket
|
|||
|
broadcast chan logic.INotice
|
|||
|
emit chan *HubEmit
|
|||
|
register chan *Websocket
|
|||
|
unregister chan *Websocket
|
|||
|
locker *sync.RWMutex
|
|||
|
}
|
|||
|
|
|||
|
type HubEmit struct {
|
|||
|
ID string
|
|||
|
Msg logic.INotice
|
|||
|
}
|
|||
|
|
|||
|
type Message struct {
|
|||
|
Prefix string `json:"prefix"`
|
|||
|
logic.INotice `json:"data"`
|
|||
|
}
|
|||
|
|
|||
|
var HubMessage *Hub
|
|||
|
|
|||
|
func (this *Hub) Run() {
|
|||
|
for {
|
|||
|
select {
|
|||
|
case client := <-this.register:
|
|||
|
this.clients[client.ID] = client
|
|||
|
logger.InfoF("客户端【%s】发起链接", client.ID)
|
|||
|
//for {
|
|||
|
// select {
|
|||
|
// case <-client.time.C:
|
|||
|
// fmt.Println("重置了")
|
|||
|
// client.time.Stop()
|
|||
|
// }
|
|||
|
//}
|
|||
|
case client := <-this.unregister:
|
|||
|
this.locker.Lock()
|
|||
|
|
|||
|
if _, ok := this.clients[client.ID]; ok {
|
|||
|
delete(this.clients, client.ID)
|
|||
|
close(client.send)
|
|||
|
}
|
|||
|
this.locker.Unlock()
|
|||
|
case message := <-this.broadcast:
|
|||
|
_message, _ := json.Marshal(&Message{
|
|||
|
Prefix: message.Prefix(),
|
|||
|
INotice: message,
|
|||
|
})
|
|||
|
for _, client := range this.clients {
|
|||
|
client.send <- _message
|
|||
|
}
|
|||
|
case iMsg := <-this.emit:
|
|||
|
client, has := this.clients[iMsg.ID]
|
|||
|
|
|||
|
if has {
|
|||
|
_message, _ := json.Marshal(&Message{
|
|||
|
Prefix: iMsg.Msg.Prefix(),
|
|||
|
INotice: iMsg.Msg,
|
|||
|
})
|
|||
|
client.send <- _message
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func (this *Hub) EmitHandle(iMsg *HubEmit) {
|
|||
|
this.emit <- iMsg
|
|||
|
}
|
|||
|
|
|||
|
func (this *Hub) BroadcastHandle(msg logic.INotice) {
|
|||
|
this.broadcast <- msg
|
|||
|
}
|
|||
|
|
|||
|
func (this *Hub) RegisterHandle(ws *Websocket) {
|
|||
|
this.register <- ws
|
|||
|
}
|
|||
|
|
|||
|
func (this *Hub) UnregisterHandle(ws *Websocket) {
|
|||
|
this.unregister <- ws
|
|||
|
}
|
|||
|
|
|||
|
func NewHub() *Hub {
|
|||
|
HubMessage = &Hub{
|
|||
|
clients: make(map[string]*Websocket),
|
|||
|
broadcast: make(chan logic.INotice),
|
|||
|
emit: make(chan *HubEmit),
|
|||
|
register: make(chan *Websocket),
|
|||
|
unregister: make(chan *Websocket),
|
|||
|
locker: new(sync.RWMutex),
|
|||
|
}
|
|||
|
return HubMessage
|
|||
|
}
|
|||
|
|
|||
|
func (this *Websocket) Write() {
|
|||
|
defer func() {
|
|||
|
this.conn.Close()
|
|||
|
}()
|
|||
|
for {
|
|||
|
select {
|
|||
|
case message, ok := <-this.send:
|
|||
|
if !ok {
|
|||
|
return
|
|||
|
}
|
|||
|
logger.InfoF("发送到客户端【%s】信息:%s", this.ID, string(message))
|
|||
|
_ = this.conn.WriteMessage(websocket.TextMessage, message)
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func (this *Websocket) Read() {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
func NewWebsocket(id string, conn *websocket.Conn) *Websocket {
|
|||
|
return &Websocket{
|
|||
|
ID: id,
|
|||
|
conn: conn,
|
|||
|
send: make(chan []byte),
|
|||
|
time: time.NewTicker(10 * time.Second),
|
|||
|
}
|
|||
|
}
|