Last active
August 7, 2019 20:53
-
-
Save wwsean08/2c5047cc9c2b276a864bf1c72f7d2989 to your computer and use it in GitHub Desktop.
This file contains 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
func sendMessage(_ js.Value, _ []js.Value) interface{} { | |
doc := js.Global().Get("document") | |
msg := doc.Call("getElementById", "message").Get("value").String() | |
user := doc.Call("getElementById", "username").Get("value").String() | |
doc.Call("getElementById", "message").Set("value", "") | |
println(user, msg) | |
location := js.Global().Get("window").Get("location") | |
scheme := "ws" | |
if location.Get("protocol").String() == "https:" { | |
scheme = "wss" | |
} | |
host := location.Get("host").String() | |
addr := fmt.Sprintf("%s://%s/ws",scheme, host) | |
fmt.Printf("Connecting to %s", addr) | |
ws := js.Global().Get("WebSocket").New(addr) | |
ws.Call("addEventListener", "open", js.FuncOf(func(this js.Value, args []js.Value) interface{} { | |
ws.Call("send", fmt.Sprintf("%s - %s", user, msg)) | |
return nil | |
})) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment