From 28356a0c8bd79c989c469e710b3c6c1b90398dcf Mon Sep 17 00:00:00 2001 From: Dheerendra Rathor Date: Mon, 9 Oct 2017 03:36:54 +0530 Subject: [PATCH] Added subprotocol match on client --- README.md | 11 ++++++++++- main.go | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b86071a..022ea1b 100644 --- a/README.md +++ b/README.md @@ -15,5 +15,14 @@ A simple websocket client in Golang to debug websocket server. ### Usages: ```bash -.\SimpleWsClient --addr=wss://echo.websocket.org +$ ./SimpleWsClient -help +Usage of ./SimpleWsClient: + -addr string + http service address (default "ws://echo.websocket.org") + -echoDelay uint + Delay before echoing back received message from server + -protocols string + Comma separated list of protocols to use + +$ ./SimpleWsClient -addr wss://echo.websocket.org -protocols echo,chat ``` diff --git a/main.go b/main.go index 8d500e6..09459eb 100644 --- a/main.go +++ b/main.go @@ -50,6 +50,23 @@ func main() { log.Fatalf("Unable to dial ws connection to agent %s with error %s\n", address, err) } + respSubProtocol := conn.Subprotocol() + subProtocolMatch := false + if len(dialer.Subprotocols) > 0 { + for _, val := range dialer.Subprotocols { + if val == respSubProtocol { + subProtocolMatch = true + break + } + } + } else { + subProtocolMatch = respSubProtocol == "" + } + + if !subProtocolMatch { + log.Fatalf("Subprotocol match failed. Server returned subprotocol: '%s'\n", respSubProtocol) + } + log.Println("Connection stabilized") defer conn.Close()