Example #1
0
// Create a websocket connection to the given path and return the connection
func (t *TestSuite) WebSocket(path string) *websocket.Conn {
	origin := t.BaseUrl() + "/"
	url := t.WebSocketUrl() + path
	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		panic(err)
	}
	return ws
}
Example #2
0
// 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])
}
Example #3
0
func connect(start time.Time, s *Stat) {
	defer wg.Done()
	c, err := websocket.Dial("ws://localhost:8000/", "", "http://localhost/")
	if err != nil {
		s.Error = "dial: " + err.Error()
		return
	}
	in := make([]byte, 3)
	out := make([]byte, len(in))
	s.Connect = time.Since(start)
	for i := 0; i < 60; i++ {
		t0 := time.Now()
		out = strconv.AppendInt(out[:0], int64(i), 10)
		n, err := c.Write(out)
		if err != nil {
			s.Error = "write: " + err.Error()
			return
		}
		if n != len(out) {
			s.Error = fmt.Sprintf("unexpected write count %d/%d", n, len(out))
			return
		}
		n, err = c.Read(in)
		t1 := time.Now()
		if err != nil {
			s.Error = "read: " + err.Error()
			return
		}
		if n != len(out) {
			s.Error = fmt.Sprintf("unexpected read data %q", in[0:n])
			return
		}
		if !bytes.Equal(out, in[0:n]) {
			s.Error = fmt.Sprintf("unexpected ping result, expect %q got %q", out, in[0:n])
			return
		}
		s.Latency = append(s.Latency, t1.Sub(t0))
		time.Sleep(1 * time.Second)
		select {
		case <-stop:
			return
		default:
		}
	}
}