Created
December 9, 2013 23:17
-
-
Save andybons/7882898 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
package main | |
import ( | |
"code.google.com/p/go.net/websocket" | |
"log" | |
"net/http" | |
) | |
func main() { | |
log.Println("server started at localhost:8081") | |
http.Handle("/close/websocket", websocket.Handler(func(ws *websocket.Conn) { | |
ws.Close() | |
})) | |
http.Handle("/echo/websocket", websocket.Handler(func(ws *websocket.Conn) { | |
for { | |
var buf string | |
err := websocket.Message.Receive(ws, &buf) | |
if err != nil { | |
log.Println(err) | |
break | |
} | |
log.Printf("Received: %q\n", buf) | |
err = websocket.Message.Send(ws, buf) | |
if err != nil { | |
log.Println(err) | |
break | |
} | |
log.Printf("Sent :%q\n", buf) | |
} | |
log.Println("Socket Closed.") | |
})) | |
log.Fatal(http.ListenAndServe(":8081", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment