-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
52 lines (41 loc) · 1.14 KB
/
server.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
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"github.com/civet148/gotools/log"
"github.com/civet148/gotools/wss"
_ "github.com/civet148/gotools/wss/tcpsock" //required (register socket instance)
)
const (
TCP_DATA_PING = "ping"
TCP_DATA_PONG = "pong"
)
type Server struct {
service *wss.SocketServer
}
func main() {
var url = "tcp://0.0.0.0:6666"
server(url)
var c = make(chan bool, 1)
<-c //block main go routine
}
func server(strUrl string) {
var server Server
server.service = wss.NewServer(strUrl)
if err := server.service.Listen(&server); err != nil {
log.Errorf(err.Error())
return
}
}
func (s *Server) OnAccept(c *wss.SocketClient) {
log.Infof("connection accepted [%v]", c.GetRemoteAddr())
}
func (s *Server) OnReceive(c *wss.SocketClient, data []byte, length int, from string) {
log.Infof("tcp server received data [%s] length [%v] from [%v]", data, length, from)
if string(data) == TCP_DATA_PING {
if _, err := c.Send([]byte(TCP_DATA_PONG)); err != nil {
log.Errorf(err.Error())
}
}
}
func (s *Server) OnClose(c *wss.SocketClient) {
log.Infof("connection [%v] closed", c.GetRemoteAddr())
}