Skip to content

Commit

Permalink
simplify code by using proper websocket api
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Jun 5, 2022
1 parent 3b55daa commit 5b76c84
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions ttapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,18 @@ func (b *Bot) startWS() {
}

func (b *Bot) readWS() {
var msg []byte
var msgLen, msgRead int
LOOP:
for {
select {
case <-b.ctx.Done():
break LOOP
default:
}
var buf = make([]byte, 1024*1024)
var buf []byte
if err := b.ws.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
logrus.Error("failed to set read deadline:", err)
}
n, err := b.ws.Read(buf)
if err != nil {
if err := websocket.Message.Receive(b.ws, &buf); err != nil {
if err == io.EOF {
logrus.Error("socket eof:", err)
break
Expand All @@ -109,18 +106,7 @@ LOOP:
break
}
}
packet := buf[0:n]
if bytes.HasPrefix(packet, []byte("~m~")) {
m := lenRgx.FindSubmatch(packet)
msgLen, _ = strconv.Atoi(string(m[1]))
msgRead = -6 - len(m[1])
msg = make([]byte, 0)
}
msg = append(msg, packet...)
msgRead += n
if msgRead == msgLen {
b.rx(msg)
}
b.rx(buf)
}
}

Expand Down

0 comments on commit 5b76c84

Please sign in to comment.