Skip to content

Commit

Permalink
fix WebSocket Listen problem for c# connector
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-tennis committed Mar 26, 2024
1 parent 1416c97 commit 192a1d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Codec interface {
// process divided into two layers
// layer1: retrieve the original package data from RingBuffer
// layer2: encode or decode of the original package data
// NOTE: only support TcpConnection
type RingBufferCodec struct {
// 包头的编码接口,包头长度不能变
// encoder for packer header
Expand Down
22 changes: 16 additions & 6 deletions ws_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ func testWebSocket(t *testing.T, protocolName, certFile, keyFile string) {
codec.Register(PacketCommand(10086), nil)

connectionHandler := NewDefaultConnectionHandler(codec)
connectionHandler.RegisterHeartBeat(func() Packet {
return NewProtoPacket(PacketCommand(pb.CmdTest_Cmd_HeartBeat), &pb.HeartBeatReq{
Timestamp: GetCurrentTimeStamp(),
})
})
connectionHandler.Register(PacketCommand(pb.CmdTest_Cmd_HeartBeat), onHeartBeatReq, new(pb.HeartBeatReq))
connectionHandler.SetUnRegisterHandler(func(connection Connection, packet Packet) {
streamStr := ""
if packet.GetStreamData() != nil {
streamStr = string(packet.GetStreamData())
}
t.Logf("%v %v %v %v", connection.GetConnectionId(), packet.Command(), packet.Message(), streamStr)
connection.SendPacket(packet.Clone())
})
acceptConnectionConfig.Codec = codec
acceptConnectionConfig.Handler = connectionHandler
Expand All @@ -72,14 +69,27 @@ func testWebSocket(t *testing.T, protocolName, certFile, keyFile string) {
listener := netMgr.NewWsListener(ctx, listenAddress, listenerConfig)
time.Sleep(time.Second)

connectorHandler := NewDefaultConnectionHandler(codec)
connectorHandler.RegisterHeartBeat(func() Packet {
return NewProtoPacket(PacketCommand(pb.CmdTest_Cmd_HeartBeat), &pb.HeartBeatReq{
Timestamp: GetCurrentTimeStamp(),
})
})
connectorHandler.SetUnRegisterHandler(func(connection Connection, packet Packet) {
streamStr := ""
if packet.GetStreamData() != nil {
streamStr = string(packet.GetStreamData())
}
t.Logf("%v %v %v %v", connection.GetConnectionId(), packet.Command(), packet.Message(), streamStr)
})
connectorConnectionConfig := ConnectionConfig{
SendPacketCacheCap: 100,
MaxPacketSize: 60,
RecvTimeout: 3,
HeartBeatInterval: 2,
WriteTimeout: 1,
Codec: codec,
Handler: connectionHandler,
Handler: connectorHandler,
Path: "/" + protocolName,
Scheme: protocolName,
}
Expand Down
12 changes: 3 additions & 9 deletions ws_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

type WsListener struct {
baseListener
httpServer *http.Server
upgrader websocket.Upgrader
upgrader websocket.Upgrader

acceptConnectionConfig ConnectionConfig
acceptConnectionCodec Codec
Expand Down Expand Up @@ -67,7 +66,6 @@ func (this *WsListener) Addr() net.Addr {
}

func (this *WsListener) Close() {
this.httpServer.Close()
}

func (this *WsListener) IsRunning() bool {
Expand All @@ -82,18 +80,14 @@ func (this *WsListener) Start(ctx context.Context, listenAddress string) bool {
ReadBufferSize: int(this.acceptConnectionConfig.RecvBufferSize),
WriteBufferSize: int(this.acceptConnectionConfig.SendBufferSize),
}
this.httpServer = &http.Server{
Addr: listenAddress,
ReadHeaderTimeout: 3 * time.Second,
}
// 监听协程
atomic.StoreInt32(&this.isRunning, 1)
go func() {
var err error
if this.config.CertFile != "" {
err = this.httpServer.ListenAndServeTLS(this.config.CertFile, this.config.KeyFile)
err = http.ListenAndServeTLS(listenAddress, this.config.CertFile, this.config.KeyFile, nil)
} else {
err = this.httpServer.ListenAndServe()
err = http.ListenAndServe(listenAddress, nil)
}
if err != nil {
atomic.StoreInt32(&this.isRunning, 0)
Expand Down

0 comments on commit 192a1d0

Please sign in to comment.