Start starts the client, sends requests and establishes a connection. (重新启动客户端,发送请求且建立连接)
()
| 130 | // Start starts the client, sends requests and establishes a connection. |
| 131 | // (重新启动客户端,发送请求且建立连接) |
| 132 | func (c *Client) Restart() { |
| 133 | //try to stop and wait until client stoped |
| 134 | c.Stop() |
| 135 | |
| 136 | //set started flag |
| 137 | c.Lock() |
| 138 | if c.started { |
| 139 | // already started, just return |
| 140 | c.Unlock() |
| 141 | return |
| 142 | } |
| 143 | c.started = true |
| 144 | c.ctx, c.cancel = context.WithCancel(context.Background()) |
| 145 | c.Add(1) |
| 146 | c.Unlock() |
| 147 | |
| 148 | zlog.Ins().InfoF("[START] Zinx Client dial RemoteAddr: %s:%d\n", c.Ip, c.Port) |
| 149 | go func() { |
| 150 | defer c.Done() |
| 151 | |
| 152 | // Create a raw socket and get net.Conn (创建原始Socket,得到net.Conn) |
| 153 | var connect ziface.IConnection |
| 154 | switch c.version { |
| 155 | case "websocket": |
| 156 | wsAddr := fmt.Sprintf("ws://%s:%d", c.Ip, c.Port) |
| 157 | if c.Url != nil { |
| 158 | wsAddr = c.Url.String() |
| 159 | } |
| 160 | |
| 161 | // Create a raw socket and get net.Conn (创建原始Socket,得到net.Conn) |
| 162 | wsConn, _, err := c.dialer.DialContext(c.ctx, wsAddr, c.WsHeader) |
| 163 | if err != nil { |
| 164 | // connection failed |
| 165 | zlog.Ins().ErrorF("WsClient connect to server failed, err:%v", err) |
| 166 | c.notifyErr(err) |
| 167 | return |
| 168 | } |
| 169 | // Create Connection object |
| 170 | connect = newWsClientConn(c, wsConn) |
| 171 | |
| 172 | default: |
| 173 | var conn net.Conn |
| 174 | var err error |
| 175 | if c.useTLS { |
| 176 | // TLS encryption |
| 177 | config := &tls.Config{ |
| 178 | // Skip certificate verification here because the CA certificate of the certificate issuer is not authenticated |
| 179 | // (这里是跳过证书验证,因为证书签发机构的CA证书是不被认证的) |
| 180 | InsecureSkipVerify: true, |
| 181 | } |
| 182 | d := &tls.Dialer{ |
| 183 | Config: config, |
| 184 | } |
| 185 | //conn, err = tls.Dial("tcp", fmt.Sprintf("%v:%v", net.ParseIP(c.Ip), c.Port), config) |
| 186 | conn, err = d.DialContext(c.ctx, "tcp", fmt.Sprintf("%v:%v", net.ParseIP(c.Ip), c.Port)) |
| 187 | if err != nil { |
| 188 | zlog.Ins().ErrorF("tls client connect to server failed, err:%v", err) |
| 189 | c.notifyErr(err) |
no test coverage detected