66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.pyer.club/kingecg/goemitter"
|
|
"git.pyer.club/kingecg/gotunnelserver/util"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
type Session struct {
|
|
Id string
|
|
conn *websocket.Conn
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
*goemitter.EventEmitter
|
|
}
|
|
|
|
type Command struct {
|
|
Type int `json:"type"`
|
|
Payload map[string]string `json:"payload"`
|
|
}
|
|
|
|
func (s *Session) Send(cmd *Command) (err error) {
|
|
return s.conn.WriteJSON(cmd)
|
|
}
|
|
func (s *Session) Close() {
|
|
s.conn.Close()
|
|
}
|
|
|
|
func (s *Session) Start() {
|
|
s.ctx, s.cancel = context.WithCancel(context.Background())
|
|
go func() {
|
|
defer s.conn.Close()
|
|
for {
|
|
var cmd Command
|
|
err := s.conn.ReadJSON(&cmd)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if cmd.Type == util.NewConnection {
|
|
s.Emit("NewSession", cmd.Payload)
|
|
}
|
|
select {
|
|
case <-s.ctx.Done():
|
|
return
|
|
default:
|
|
continue
|
|
}
|
|
}
|
|
}()
|
|
|
|
}
|
|
|
|
func (s *Session) Stop() {
|
|
s.cancel()
|
|
}
|
|
|
|
func NewSession(conn *websocket.Conn) *Session {
|
|
return &Session{
|
|
conn: conn,
|
|
Id: util.GenRandomstring(16),
|
|
EventEmitter: goemitter.NewEmitter(),
|
|
}
|
|
}
|