func TestClientServer(t *testing.T) {
	s := httptest.NewServer(wsHandler{t})
	defer s.Close()
	u, _ := url.Parse(s.URL)
	c, err := net.Dial("tcp", u.Host)
	if err != nil {
		t.Fatalf("Dial: %v", err)
	}
	ws, _, err := websocket.NewClient(c, u, http.Header{"Origin": {s.URL}}, 1024, 1024)
	if err != nil {
		t.Fatalf("NewClient: %v", err)
	}
	defer ws.Close()
	w, _ := ws.NextWriter(websocket.OpText)
	io.WriteString(w, "HELLO")
	w.Close()
	ws.SetReadDeadline(time.Now().Add(1 * time.Second))
	op, r, err := ws.NextReader()
	if err != nil {
		t.Fatalf("NextReader: %v", err)
	}
	if op != websocket.OpText {
		t.Fatalf("op=%d, want %d", op, websocket.OpText)
	}
	b, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}
	if string(b) != "HELLO" {
		t.Fatalf("message=%s, want %s", b, "HELLO")
	}
}
Exemple #2
0
func newClient(u *url.URL, method string) (*websocket.Conn, *http.Response, error) {
	var c net.Conn
	var err error
	hostport := hostPort(u)
	switch u.Scheme {
	case "http":
		c, err = net.Dial("tcp", hostport)
	case "https":
		c, err = tls.Dial("tcp", hostport, nil)
	default:
		return nil, nil, errors.New("unrecognized URL scheme")
	}
	if err != nil {
		return nil, nil, err
	}
	return websocket.NewClient(c, u, http.Header{xVerb: []string{method}}, readBufSize, writeBufSize)
}
func TestClientServer(t *testing.T) {
	s := httptest.NewServer(wsHandler{t})
	defer s.Close()
	u, _ := url.Parse(s.URL)
	c, err := net.Dial("tcp", u.Host)
	if err != nil {
		t.Fatalf("Dial: %v", err)
	}
	ws, resp, err := websocket.NewClient(c, u, http.Header{"Origin": {s.URL}}, 1024, 1024)
	if err != nil {
		t.Fatalf("NewClient: %v", err)
	}
	defer ws.Close()

	var sessionID string
	for _, c := range resp.Cookies() {
		if c.Name == "sessionID" {
			sessionID = c.Value
		}
	}
	if sessionID != "1234" {
		t.Error("Set-Cookie not received from the server.")
	}

	w, _ := ws.NextWriter(websocket.TextMessage)
	io.WriteString(w, "HELLO")
	w.Close()
	ws.SetReadDeadline(time.Now().Add(1 * time.Second))
	op, r, err := ws.NextReader()
	if err != nil {
		t.Fatalf("NextReader: %v", err)
	}
	if op != websocket.TextMessage {
		t.Fatalf("op=%d, want %d", op, websocket.TextMessage)
	}
	b, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}
	if string(b) != "HELLO" {
		t.Fatalf("message=%s, want %s", b, "HELLO")
	}
}
func (self *Client) Connect(ups *UplinkScenario) {
	// var err error
	c, err := net.Dial("tcp", fmt.Sprintf("%s:%d", self.Stream.Hostname, self.Stream.Port))
	if err != nil {
		log.Println("in initialize ws 1", err)
	}
	u, _ := url.Parse(fmt.Sprintf("ws://%s:%d/websocket", self.Stream.Hostname, self.Stream.Port))
	// connected as game client
	// self.ws, err = websocket.Dial(fmt.Sprintf("ws://%s:%d/websocket", self.Stream.Hostname, self.Stream.Port),
	// "", fmt.Sprintf("http://%s:%d", self.Stream.Hostname, self.Stream.Port))
	ws, _, err := websocket.NewClient(c, u, http.Header{"Origin": {fmt.Sprintf("%s:%d", self.Stream.Hostname, self.Stream.Port+30000)}}, 1024, 1024)
	if err != nil {
		log.Println("in initialize ws", err)
		self.Close()
		return
	}
	// self.socket = socket.NewSocket(conn)

	// Subscribe on stream.
	da, _ := json.Marshal(&message.Outgoing{
		Type: "subscribe",
		Payload: struct {
			ID    string `json:"id"`
			Hub   string `json:"hub"`
			Token string `json:"token"`
		}{
			ID:    self.id,
			Hub:   self.hub,
			Token: self.Token,
		},
	})
	// _, err = self.ws.Write(da)
	// err = websocket.Message.Send(self.ws, da)
	w, err := ws.NextWriter(websocket.OpText)
	if err != nil {
		log.Println("in sending ws", err)
		self.Close()
		return
	}
	io.WriteString(w, string(da))
	w.Close()
	// ws.SetReadDeadline(time.Now().Add(1000000 * time.Second))
	ssd := new(struct {
		Type    string `json:"type"`
		Payload int64  `json:"payload"`
	})

	go func() {
		// var im []byte

		for {
			_, r, err := ws.NextReader()
			if err != nil {
				log.Println("NextReader: %v", err)
				// break
			}
			b, err := ioutil.ReadAll(r)
			if err != nil {
				log.Println("ReadAll: %v", err)
				break
			}
			// log.Println(string(b))
			r = bytes.NewReader(b)
			json.NewDecoder(r).Decode(&ssd)
			if ssd.Payload != 0 {
				gap := time.Now().UnixNano() - ssd.Payload
				switch ssd.Type {
				case "send":
					atomic.AddInt64(&ups._totalSendCount, 1)
					atomic.AddInt64(&ups._totalSendReceiveTime, gap)
				case "pub":
					atomic.AddInt64(&ups._totalPubCount, 1)
					atomic.AddInt64(&ups._totalPubReceiveTime, gap)
				}

			}
		}

		self.Close()
		// log.Println(fmt.Sprintf("%2.2f%s", (float64(ups._totalReceiveTime) / float64(ups._totalSend)), "s"))

	}()
}