示例#1
0
func publisher(chTrigger chan int, chTime chan time.Time, url, origin, connectMessage, subscribeMessage, publishMessage string) {
	var err error
	var ws *websocket.Conn
	for {
		ws, err = websocket.Dial(url, "", origin)
		if err != nil {
			time.Sleep(50 * time.Millisecond)
			continue
		}
		break
	}

	var msg = make([]byte, 512)

	if _, err := ws.Write([]byte(connectMessage)); err != nil {
		fmt.Println("publisher connect write error")
		log.Fatal(err)
	}
	if _, err = ws.Read(msg); err != nil {
		fmt.Println("publisher connect read error")
		log.Fatal(err)
	}

	if _, err := ws.Write([]byte(subscribeMessage)); err != nil {
		fmt.Println("publisher subscribe write error")
		log.Fatal(err)
	}
	if _, err = ws.Read(msg); err != nil {
		fmt.Println("publisher subscribe read error")
		log.Fatal(err)
	}

	for {
		<-chTrigger

		if _, err := ws.Write([]byte(publishMessage)); err != nil {
			fmt.Println("publisher publish write error")
			log.Fatal(err)
		}

		chTime <- time.Now()

		if _, err = ws.Read(msg); err != nil {
			fmt.Println("publisher publish read error")
			log.Fatal(err)
		}

		if _, err = ws.Read(msg); err != nil {
			fmt.Println("publisher message read error")
			log.Fatal(err)
		}

	}
}
示例#2
0
func subscriber(numChannels int, chSub chan struct{}, url, origin, connectMessage string) {
	var err error
	var ws *websocket.Conn
	for {
		ws, err = websocket.Dial(url, "", origin)
		if err != nil {
			time.Sleep(10 * time.Millisecond)
			continue
		}
		break
	}
	if _, err := ws.Write([]byte(connectMessage)); err != nil {
		fmt.Println("subscriber connect write error")
		log.Fatal(err)
	}
	var msg = make([]byte, 512)

	if _, err = ws.Read(msg); err != nil {
		fmt.Println("subscriber connect read error")
		log.Fatal(err)
	}

	for i := 1; i <= numChannels; i++ {
		channel := fmt.Sprintf("channel%d", i)
		subscribeMessage := "{\"params\": {\"channel\": \"" + channel + "\"}, \"method\": \"subscribe\"}"
		if _, err := ws.Write([]byte(subscribeMessage)); err != nil {
			fmt.Println("subscriber subscribe write error")
			log.Fatal(err)
		}
		if _, err = ws.Read(msg); err != nil {
			fmt.Println("subscriber subscribe read error")
			log.Fatal(err)
		}
	}

	close(chSub)

	for {
		if _, err = ws.Read(msg); err != nil {
			fmt.Println("subscriber msg read error")
			log.Fatal(err)
		}
	}
}
示例#3
0
func subscriber(chSub, chMsg, chStart chan int, url, origin, connectMessage, subscribeMessage, publishMessage string) {
	var err error
	var ws *websocket.Conn
	for {
		ws, err = websocket.Dial(url, "", origin)
		if err != nil {
			time.Sleep(10 * time.Millisecond)
			continue
		}
		break
	}
	if _, err := ws.Write([]byte(connectMessage)); err != nil {
		fmt.Println("subscriber connect write error")
		log.Fatal(err)
	}
	var msg = make([]byte, 512)

	if _, err = ws.Read(msg); err != nil {
		fmt.Println("subscriber connect read error")
		log.Fatal(err)
	}

	if _, err := ws.Write([]byte(subscribeMessage)); err != nil {
		fmt.Println("subscriber subscribe write error")
		log.Fatal(err)
	}
	if _, err = ws.Read(msg); err != nil {
		fmt.Println("subscriber subscribe read error")
		log.Fatal(err)
	}

	chSub <- 1

	for {
		if _, err = ws.Read(msg); err != nil {
			fmt.Println("subscriber msg read error")
			log.Fatal(err)
		}
		//fmt.Println("message received")
		chMsg <- 1
	}
}
示例#4
0
func subscriber(chSub chan struct{}, url, origin, connectMessage, subscribeMessage string) {
	var err error
	var ws *websocket.Conn
	for {
		ws, err = websocket.Dial(url, "", origin)
		if err != nil {
			time.Sleep(10 * time.Millisecond)
			continue
		}
		break
	}
	if _, err := ws.Write([]byte(connectMessage)); err != nil {
		fmt.Println("subscriber connect write error")
		log.Fatal(err)
	}
	var msg = make([]byte, 512)

	if _, err = ws.Read(msg); err != nil {
		fmt.Println("subscriber connect read error")
		log.Fatal(err)
	}

	if _, err := ws.Write([]byte(subscribeMessage)); err != nil {
		fmt.Println("subscriber subscribe write error")
		log.Fatal(err)
	}
	if _, err = ws.Read(msg); err != nil {
		fmt.Println("subscriber subscribe read error")
		log.Fatal(err)
	}

	close(chSub)

	for {
		if _, err = ws.Read(msg); err != nil {
			fmt.Println("subscriber msg read error")
			log.Fatal(err)
		}
	}
}