ws_client.go 3.63 KB
Newer Older
haoyanbin's avatar
haoyanbin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package main

import (
	"flag"
	"fmt"
	"github.com/gorilla/websocket"
	"log"
	"net/http"
	"net/url"
	"os"
	"os/signal"
	"pool/pool"
	"runtime"
	"time"
)

haoyanbin's avatar
haoyanbin committed
17
var addr2 = flag.String("addr", "127.0.0.1:11001", "http service address")
haoyanbin's avatar
haoyanbin committed
18 19 20

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
haoyanbin's avatar
haoyanbin committed
21
	for i := 1; i < 50; i++ {
haoyanbin's avatar
haoyanbin committed
22
		go wsClient2(fmt.Sprintf("lA6fUNMamyUBlOokPOeiGg==_1_%d", i))
haoyanbin's avatar
haoyanbin committed
23 24 25 26 27 28 29
	}
	select {}
}

func wsClient2(id string) {
	flag.Parse()
	log.SetFlags(0)
haoyanbin's avatar
haoyanbin committed
30 31 32
	//list := strings.Split(id, "_")
	//Id := "lA6fUNMamyUBlOokPOeiGg==_1_" + list[2]
	//chann := list[1:]
haoyanbin's avatar
haoyanbin committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)

	u := url.URL{Scheme: "ws", Host: *addr2, Path: "/ws"}
	log.Printf("connecting to %s", u.String())
	head := http.Header{}
	log.Printf("connecting info: %s", id)
	head.Add("Sec-Websocket-Protocol", id)
	c, _, err := websocket.DefaultDialer.Dial(u.String(), head)
	if err != nil {
		log.Fatal("dial:", err)
	}
	defer func() {
		c.Close()
		//重新连接
		//t := grand.N(10, 20)
		//time.Sleep(time.Duration(t) * time.Second)
haoyanbin's avatar
haoyanbin committed
50
		//go WsClient(id)
haoyanbin's avatar
haoyanbin committed
51 52 53 54 55 56 57 58
	}()
	ping := make(chan int)
	c.SetPingHandler(func(appData string) error {
		ping <- 1
		return nil
	})

	done := make(chan struct{})
haoyanbin's avatar
haoyanbin committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

	msg1 := &pool.SendMsg{
		ToClientId:    "",
		FromClientId:  id,
		ProcedureType: 1,
		SendTime: time.Now().Format("2006-01-02 15:04:05"),
		Msg:           "test" + time.Now().Format("2006-01-02 15:04:05"),
	}
	m1 := pool.SerializeJson(msg1)

	fmt.Println(1)
	err = c.WriteMessage(websocket.BinaryMessage, m1)
	if err != nil {
		fmt.Println("write:", err.Error())
		return
	}

	time.Sleep(5*time.Second)

	msg2 := &pool.SendMsg{
		ToClientId:    "",
		FromClientId:  id,
		ProcedureType: 2,
		SendTime: time.Now().Format("2006-01-02 15:04:05"),
		Msg:           "{\"petName\":\"gogogo\",\"petAge\":\"1.1\",\"question\":\"eye\"}",
	}
	m2 := pool.SerializeJson(msg2)
	fmt.Println(2)
	err = c.WriteMessage(websocket.BinaryMessage, m2)
	if err != nil {
		fmt.Println("write:", err.Error())
		return
	}

	time.Sleep(5*time.Second)

	msg3 := &pool.SendMsg{
		ToClientId:    id,
		FromClientId:  "6_2_14",
		ProcedureType: 3,
		SendTime: time.Now().Format("2006-01-02 15:04:05"),
	}
	m3 := pool.SerializeJson(msg3)
	fmt.Println(3)
	err = c.WriteMessage(websocket.BinaryMessage, m3)
	if err != nil {
		fmt.Println("write:", err.Error())
		return
	}
haoyanbin's avatar
haoyanbin committed
108

haoyanbin's avatar
haoyanbin committed
109
	ticker := time.NewTicker(10 * time.Second)
haoyanbin's avatar
haoyanbin committed
110 111 112 113 114 115 116 117 118
	ticker1 := time.NewTicker(20 * time.Second)

	defer ticker.Stop()
	for {
		select {
		case <-done:
			return
		case <-ticker.C:
			msg := &pool.SendMsg{
haoyanbin's avatar
haoyanbin committed
119 120 121
				ToClientId:    "6_2_14",
				FromClientId:  id,
				ProcedureType: 6,
haoyanbin's avatar
haoyanbin committed
122
				SendTime: time.Now().Format("2006-01-02 15:04:05"),
haoyanbin's avatar
haoyanbin committed
123
				Msg:           "test" + time.Now().Format("2006-01-02 15:04:05"),
haoyanbin's avatar
haoyanbin committed
124 125
			}
			m := pool.SerializeJson(msg)
haoyanbin's avatar
haoyanbin committed
126
			fmt.Println(6)
haoyanbin's avatar
haoyanbin committed
127 128
			err := c.WriteMessage(websocket.BinaryMessage, m)
			if err != nil {
haoyanbin's avatar
haoyanbin committed
129
				fmt.Println("write:", err.Error())
haoyanbin's avatar
haoyanbin committed
130 131 132
				return
			}
		case <-interrupt:
haoyanbin's avatar
haoyanbin committed
133
			fmt.Println("interrupt")
haoyanbin's avatar
haoyanbin committed
134 135 136 137 138

			// Cleanly close the connection by sending a close message and then
			// waiting (with timeout) for the server to close the connection.
			err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
			if err != nil {
haoyanbin's avatar
haoyanbin committed
139
				fmt.Println("write close:", err.Error())
haoyanbin's avatar
haoyanbin committed
140 141 142 143 144 145 146 147 148 149
				return
			}
			select {
			case <-done:
			case <-time.After(time.Second):
			}
			return
		case <-ping:
			err := c.WriteMessage(websocket.PongMessage, nil)
			if err != nil {
haoyanbin's avatar
haoyanbin committed
150
				fmt.Println("write pong:", err.Error())
haoyanbin's avatar
haoyanbin committed
151 152 153 154 155
				return
			}
		case <-ticker1.C:
			err := c.WriteMessage(websocket.PingMessage, nil)
			if err != nil {
haoyanbin's avatar
haoyanbin committed
156
				fmt.Println("write pong:", err.Error())
haoyanbin's avatar
haoyanbin committed
157 158 159 160 161 162
				return
			}
			//return
		}
	}
}