Last active
April 16, 2016 23:18
-
-
Save edrex/105a4c9577aa13c7c2f52048be3295ce to your computer and use it in GitHub Desktop.
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
| localhost | |
| { | |
| log proxy.log | |
| proxy /echo localhost:8080 { | |
| websocket | |
| } | |
| } |
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
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"/> | |
| </head> | |
| <body> | |
| <script> | |
| function testEchoSocket(path) { | |
| var s = new WebSocket('ws://'+location.hostname+':'+location.port + path); | |
| s.onmessage = function (event) { | |
| console.log(event.data); | |
| } | |
| s.onopen = function (event) { | |
| s.send("ohai"); | |
| } | |
| } | |
| testEchoSocket('/echo'); | |
| </script> | |
| </body> | |
| </html> |
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 ( | |
| "flag" | |
| "fmt" | |
| "github.com/gorilla/websocket" | |
| "log" | |
| "net/http" | |
| ) | |
| var addr = flag.String("addr", "localhost:8080", "http service address") | |
| var upgrader = websocket.Upgrader{ | |
| CheckOrigin: func(r *http.Request) bool { return true }, | |
| } | |
| func echo(w http.ResponseWriter, r *http.Request) { | |
| fmt.Println("Up and ahoy2") | |
| c, err := upgrader.Upgrade(w, r, nil) | |
| if err != nil { | |
| log.Print("upgrade:", err) | |
| return | |
| } | |
| defer c.Close() | |
| for { | |
| mt, message, err := c.ReadMessage() | |
| if err != nil { | |
| log.Println("read:", err) | |
| break | |
| } | |
| log.Printf("recv: %s", message) | |
| err = c.WriteMessage(mt, message) | |
| if err != nil { | |
| log.Println("write:", err) | |
| break | |
| } | |
| } | |
| } | |
| func main() { | |
| flag.Parse() | |
| log.SetFlags(0) | |
| fmt.Println("Up and ahoy") | |
| fs := http.FileServer(http.Dir("./")) | |
| http.Handle("/", fs) | |
| http.HandleFunc("/echo", echo) | |
| log.Fatal(http.ListenAndServe(*addr, nil)) | |
| } |
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
| Up and ahoy | |
| Up and ahoy2 | |
| recv: ohai | |
| signal: interrupt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

