func TestConnect(t *testing.T) { ws_, err := websocket.Dial("ws://localhost:9771/echo", "ws", "http://localhost/") ws = ws_ if err != nil { t.Error(err) } }
func websocketDial(t *testing.T) *websocket.Conn { ws, err := websocket.Dial("ws://127.0.0.1:9080/test", "ws", "http://127.0.0.1/") if err != nil { fmt.Printf("%v\n", err) os.Exit(1) } return ws }
func pub() { ws, err := websocket.Dial("ws://127.0.0.1:8080/broadcast", "", "") if err != nil { panic(err) } println("About to write") if _, err := ws.Write([]byte(message)); err != nil { panic(err) } }
func (wc *WebsocketClient) Dial(rawurl string, origin string) (err os.Error) { var messages []Message var nr int if wc.connected { return ErrConnected } if wc.ws, err = websocket.Dial(rawurl, "", origin); err != nil { return } // read handshake buf := make([]byte, 2048) if nr, err = wc.ws.Read(buf); err != nil { wc.ws.Close() return os.NewError("Dial: " + err.String()) } wc.decBuf.Write(buf[0:nr]) if messages, err = wc.dec.Decode(); err != nil { wc.ws.Close() return os.NewError("Dial: " + err.String()) } if len(messages) != 1 { wc.ws.Close() return os.NewError("Dial: expected exactly 1 message, but got " + strconv.Itoa(len(messages))) } // TODO: Fix me: The original Socket.IO codec does not have a special encoding for handshake // so we should just assume that the first message is the handshake. // The old codec should be gone pretty soon (waiting for 0.7 release) so this might suffice // until then. if _, ok := wc.codec.(SIOCodec); !ok { if messages[0].Type() != MessageHandshake { wc.ws.Close() return os.NewError("Dial: expected handshake, but got " + messages[0].Data()) } } wc.sessionid = SessionID(messages[0].Data()) if wc.sessionid == "" { wc.ws.Close() return os.NewError("Dial: received empty sessionid") } wc.connected = true go wc.reader() return }
func main() { ws, err := websocket.Dial("ws://localhost/ws", "", "http://localhost/") if err != nil { panic("Dial: " + err.String()) } if _, err := ws.Write([]byte("hello, world!\n")); err != nil { panic("Write: " + err.String()) } var msg = make([]byte, 512) var n int if n, err = ws.Read(msg); err != nil { } fmt.Println(msg[:n]) }
func TestEvents(t *testing.T) { event := DataEvent{Event: "my-custom-event", Channel: "wrong", Data: map[string]string{"foo": "bar"}} sendAndCheck(t, event) var response ErrorEvent err := websocket.JSON.Receive(ws, &response) if err != nil { t.Error(err) } if response.Error != "invalid_channel" { t.Errorf("Expected `invalid_channel` error, `%s` given", response.Error) } event.Channel = "test" ws2, err := websocket.Dial("ws://localhost:9771/echo", "ws", "http://localhost/") if err != nil { t.Error(err) } subscr := ChanneledEvent{Event: "subscribe", Channel: "test"} err = websocket.JSON.Send(ws2, subscr) if err != nil { t.Error(err) } var response2 NamedEvent err = websocket.JSON.Receive(ws2, &response2) if response2.Event != "ok" { t.Errorf("Expected `ok` event, `%s` given", response2.Event) } sendAndCheck(t, event) checkOkResponse(t) var recvEvent DataEvent err = websocket.JSON.Receive(ws2, &recvEvent) if err != nil { t.Error(err) } if recvEvent.Event != "my-custom-event" || recvEvent.Data["foo"] != "bar" { t.Errorf("Received event differs from original one (%s) != (%s)", recvEvent, event) } err = websocket.JSON.Send(ws2, event) if err != nil { t.Error(err) } err = websocket.JSON.Receive(ws2, &response) if err != nil { t.Error(err) } if response.Error != "access_denied" { t.Errorf("Expected `access_denied` error, `%s` given", response.Error) } }
// This example demonstrates a trivial client. func ExampleDial() { origin := "http://localhost/" url := "ws://localhost:12345/ws" ws, err := websocket.Dial(url, "", origin) if err != nil { log.Fatal(err) } if _, err := ws.Write([]byte("hello, world!\n")); err != nil { log.Fatal(err) } var msg = make([]byte, 512) var n int if n, err = ws.Read(msg); err != nil { log.Fatal(err) } fmt.Printf("Received: %s.\n", msg[:n]) }
func setupClient(clientID int) (*websocket.Conn, error) { var ws *websocket.Conn var err error url := fmt.Sprintf("ws://127.0.0.1:8080/redwood/1/%d", clientID) for timeout := 1; ; timeout *= 2 { ws, err = websocket.Dial(url, "", "http://127.0.0.1") if err == nil { return ws, nil } if timeout <= 20 { log.Printf("Trying to connect to websocket failed, sleeping %ds", timeout) time.Sleep(time.Duration(timeout) * time.Second) } else { return nil, err } } }
func (wc *WebsocketClient) Dial(rawurl string, origin string) (err os.Error) { var messages []Message var nr int if wc.connected { return ErrConnected } if wc.ws, err = websocket.Dial(rawurl, "", origin); err != nil { return } // read handshake buf := make([]byte, 2048) if nr, err = wc.ws.Read(buf); err != nil { wc.ws.Close() return os.NewError("Dial: " + err.String()) } wc.decBuf.Write(buf[0:nr]) if messages, err = wc.dec.Decode(); err != nil { wc.ws.Close() return os.NewError("Dial: " + err.String()) } if len(messages) != 1 { wc.ws.Close() return os.NewError("Dial: expected exactly 1 message, but got " + strconv.Itoa(len(messages))) } if messages[0].Type() != MessageHandshake { wc.ws.Close() return os.NewError("Dial: expected handshake, but got " + messages[0].Data()) } wc.sessionid = SessionID(messages[0].Data()) if wc.sessionid == "" { wc.ws.Close() return os.NewError("Dial: received empty sessionid") } wc.connected = true go wc.reader() return }
func main() { flag.Parse() if len(*host) == 0 { fmt.Print("Error: Needs -host parameter.\n") os.Exit(1) } ws, err := websocket.Dial(*host, "", *origin) if err != nil { fmt.Printf("Error: %s\n", err.String()) os.Exit(1) } rw := bufio.NewReadWriter(bufio.NewReader(ws), bufio.NewWriter(ws)) stdin := bufio.NewReader(os.Stdin) go func() { for { s, error := rw.ReadString('\n') s = Strip(s) if error != nil { fmt.Printf("Error: %s\n", error.String()) os.Exit(1) } if len(s) == 0 { continue } fmt.Printf("%s\n", s) } }() for { input, _ := stdin.ReadString('\n') rw.WriteString(input) rw.Flush() } }
func do_test(w *WSBench, msg []byte) { for i := 0; i < w.Connections; i++ { start := time.Nanoseconds() var path string if w.Type == "broadcast" { path = w.Type } else { path = "echo" } ws, err := websocket.Dial("ws://0.0.0.0:5555/"+path, "", "http://0.0.0.0/") if err != nil { panic("Dial failed: " + err.String()) } if _, err := ws.Write([]byte(msg)); err != nil { panic("Write failed: " + err.String()) } if i%1000 == 0 { fmt.Println(i) } // fmt.Println(bytes.Equal(response, response)) var response = make([]byte, 512) // fmt.Println(bytes.Equal(response, response)) n, err := ws.Read(response) if err != nil { panic("Read failed: " + err.String()) } if !bytes.Equal(msg, response[0:n]) { panic("Message not the same: " + err.String()) } ws.Close() delta := time.Nanoseconds() - start // Adding Dummy result for now. w.Ch <- Result{time: delta} } close(w.Ch) }
func main() { c := make(chan int) address := ":" + getenv("PORT", "8080") fmt.Printf("Address: %s\n", address) go func() { http.Handle("/echo", websocket.Handler(EchoServer)) error := http.ListenAndServe(address, nil) if error != nil { panic("ListenAndServe: " + error.String()) } c <- 0 }() go func() { url := "ws://localhost" + address + "/echo" ws, error := websocket.Dial(url, "", "http://localhost/") if error != nil { panic("Dial:" + error.String()) } if _, error := ws.Write([]byte("Hello world")); error != nil { panic("Write: " + error.String()) } var msg = make([]byte, 16384) n, error := ws.Read(msg) if error != nil { panic("Read: " + error.String()) } fmt.Printf("%s\n", msg[0:n]) }() /* Rough equivalent to 'thread.join' waiting for the server to terminate, if ever */ <-c }
func main() { println(1) ws, err := websocket.Dial("ws://localhost:8080/", "", "http://localhost:8080/") if err != nil { panic("Dial: " + err.String()) } println(2) if _, err := ws.Write([]byte("hello, world!\n")); err != nil { panic("Write: " + err.String()) } var msg = make([]byte, 512) println(3) if output, err := ws.Read(msg); err != nil { panic("Read: " + err.String()) } else { println(4) fmt.Printf("msg: %s\n", msg) fmt.Printf("output: %v\n", output) } println(5) // use msg[0:n] }
func TestEcho(t *testing.T) { once.Do(startServer) msg := []byte("hello, world!") ws, err := websocket.Dial("ws://0.0.0.0:5555/echo", "", "http://0.0.0.0/") if err != nil { t.Errorf("WebSocket handshake: %v", err) } if _, err := ws.Write([]byte(msg)); err != nil { t.Errorf("Write: error %v", err) } var response = make([]byte, 512) fmt.Println(bytes.Equal(response, response)) n, err := ws.Read(response) if err != nil { t.Errorf("Read: error %v", err) } if !bytes.Equal(msg, response[0:n]) { t.Errorf("Echo: expected %q got %q", msg, response) } ws.Close() }
func main() { count := 3 for i := 0; i < count; i++ { ws1, err := websocket.Dial("ws://127.0.0.1:8080/broadcast", "", "") if err != nil { panic(err) } go sub(ws1) } pub() l := list.New() for { res := <-ch l.PushBack(1) fmt.Println("Received: %v, %+v", l.Len(), res) if l.Len() >= count { fmt.Println("Got all: %v, %+v", l.Len(), res) return } } }
func wsdial(port int) (*websocket.Conn, error) { url := fmt.Sprintf("ws://127.0.0.1:%d/test", port) return websocket.Dial(url, "ws", "http://127.0.0.1/") }