-
-
Save zue666/21e5e7219b7b15bf7471a3d27d90eb92 to your computer and use it in GitHub Desktop.
golang websocket example with basic auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"encoding/json" | |
"golang.org/x/net/websocket" | |
"fmt" | |
) | |
type ClientHello struct { | |
messageType string | |
protocolVersion string | |
clientVersion string | |
} | |
func main() { | |
config, err := websocket.NewConfig("wss://sandbox/websocket", "wss://sandbox/websocket") | |
if err != nil { | |
log.Fatal(err) | |
} | |
config.Header = http.Header { | |
"Authorization" : {"Basic blabla=="}, | |
} | |
ws, err := websocket.DialConfig(config) | |
if err != nil { | |
log.Fatal(err) | |
} | |
clientHello := ClientHello { | |
"client-hello", | |
"1", | |
"4.0", | |
} | |
msg, err := json.Marshal(clientHello) | |
if err != nil { | |
log.Fatal(err) | |
} | |
ws.Write(msg) | |
var retMsg = make([]byte, 512) | |
var n int | |
n, err = ws.Read(retMsg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Received: %s.\n", retMsg[:n]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment