Skip to content

Instantly share code, notes, and snippets.

@wwsean08
Last active August 7, 2019 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwsean08/2c5047cc9c2b276a864bf1c72f7d2989 to your computer and use it in GitHub Desktop.
Save wwsean08/2c5047cc9c2b276a864bf1c72f7d2989 to your computer and use it in GitHub Desktop.
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