From c5dcaebd906ad7d2a86217664283193caefa735c Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Fri, 21 Sep 2018 13:28:34 +0800 Subject: [PATCH] Support match protocol versions between server and client Signed-off-by: Jianhui Zhao --- client.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index bb19b82..3c25502 100644 --- a/client.go +++ b/client.go @@ -23,11 +23,14 @@ import ( "time" "sync" "errors" + "strconv" "net/http" "github.com/gorilla/websocket" ) const ( + RTTY_PROTO_VERSION = 1 + // pings period of client. pingPeriod = 5 * time.Second @@ -176,12 +179,23 @@ func (c *Client) keepAlive() { /* serveWs handles websocket requests from the peer. */ func serveWs(br *Broker, w http.ResponseWriter, r *http.Request) { + isDev := r.URL.Query().Get("device") != "" devid := r.URL.Query().Get("devid") + proto := r.URL.Query().Get("proto") + if devid == "" { rlog.Println("devid required") return } + if isDev { + proto_num, err := strconv.Atoi(proto) + if err != nil || proto_num != RTTY_PROTO_VERSION { + rlog.Printf("proto number is not matched for device '%s', you need to update your server(rttys) or client(rtty) or both them", devid) + return + } + } + conn, err := upgrader.Upgrade(w, r, nil) if err != nil { rlog.Println(err) @@ -198,8 +212,7 @@ func serveWs(br *Broker, w http.ResponseWriter, r *http.Request) { isClosed: false, } - isDev := r.URL.Query().Get("device") - if isDev == "1" { + if isDev { client.isDev = true client.description = r.URL.Query().Get("description") client.cmd = make(map[uint32]chan *wsMessage)