Created
November 22, 2011 12:23
-
-
Save vmihailenco/1385547 to your computer and use it in GitHub Desktop.
WebSocket client
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 ( | |
| "os" | |
| "fmt" | |
| "websocket" | |
| ) | |
| const ( | |
| NumProcs = 5 | |
| ) | |
| var writeChan = make(chan *websocket.Conn, 5000) | |
| var readChan = make(chan *websocket.Conn, 5000) | |
| func write(in <-chan *websocket.Conn, out chan<- *websocket.Conn) { | |
| for ws := range in { | |
| if _, err := ws.Write([]byte("message")); err != nil { | |
| fmt.Sprintf("Write: " + err.String()) | |
| } | |
| out <- ws | |
| } | |
| } | |
| func read(in <-chan *websocket.Conn, out chan<- *websocket.Conn) { | |
| buf := make([]byte, 512) | |
| for ws := range in { | |
| buf = buf[:512] | |
| n, err := ws.Read(buf) | |
| if err != nil { | |
| if err != os.EOF { | |
| fmt.Printf("clientHandler.for: %v\n", err) | |
| } | |
| break | |
| } | |
| if string(buf[:n]) != "message" { | |
| fmt.Printf("Received wrong message") | |
| } | |
| out <- ws | |
| } | |
| } | |
| func main() { | |
| for i := 0; i < NumProcs; i++ { | |
| go write(writeChan, readChan) | |
| } | |
| for i := 0; i < NumProcs; i++ { | |
| go read(readChan, writeChan) | |
| } | |
| go func() { | |
| for i := 0; i < 5000; i++ { | |
| ws, err := websocket.Dial("ws://localhost:9999/", "", "http://localhost:9999/") | |
| if err != nil { | |
| fmt.Sprintf("Dial: " + err.String()) | |
| } | |
| writeChan <- ws | |
| } | |
| }() | |
| ch := make(chan int) | |
| ch <- 1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment