Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pedrolopesme
Last active March 11, 2018 05:18
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 pedrolopesme/9082f804d38a99784fec4fec7b7a67f3 to your computer and use it in GitHub Desktop.
Save pedrolopesme/9082f804d38a99784fec4fec7b7a67f3 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/gorilla/websocket"
"net/http"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}
func handler(writer http.ResponseWriter, request *http.Request) {
socket, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
fmt.Println(err)
}
for {
// Vamos ler a mensagem recebida via Websocket
msgType, msg, err := socket.ReadMessage()
if err != nil {
fmt.Println(err)
return
}
// Logando no console do Webserver
fmt.Println("Mensagem recebida: ", string(msg))
// Devolvendo a mensagem recebida de volta para o cliente
err = socket.WriteMessage(msgType, msg)
if err != nil {
fmt.Println(err)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment