Beispiel #1
0
func CloseConn(conn *net.TCPConn, sid string) {

	conn.Write([]byte{'E', 'O', 'F'})
	conn.Close()
	log.Warn("Sid closing:", sid)
	locks.CloseConn(sid)
	locks.CloseSession(sid)
	locks.CloseUserChannel(sid)
	atomic.AddInt32(&global.SumConnections, -1)
	area.UserUnSubscribeChannel(sid)
	log.Info("UserConns length:", len(global.UserConns))

}
Beispiel #2
0
/**
 *  处理客户端连接
 */
func Websocket_server(ws *websocket.Conn) {

	var err error

	atomic.AddInt32(&global.SumConnections, 1)
	user_sid := ""
	for {
		var str string

		if err = websocket.Message.Receive(ws, &str); err != nil {

			log.Error(" websocket.Message.Receive error:", user_sid, "  -->", err.Error())
			if err.Error() == "EOF" {

				log.Error(" websocket clear !")
				delete(global.UserWebsocketConns, user_sid)

				locks.CloseWsConn(user_sid)
				locks.CloseSession(user_sid)
				locks.CloseUserChannel(user_sid)
				area.UserUnSubscribeChannel(user_sid)

			}
			atomic.AddInt32(&global.SumConnections, -1)
			ws.Close()
			break

		}

		log.Debug("Client Request: " + str)

		go func() {

			if str != "" {

				global.WorkerNbrs = global.WorkerNbrs[1:]
				/*
					worker_nbr := global.WorkerNbrs[0]
					// Requests
					global.CheckError(err)
					websocket.Message.Send(ws, msg.Data)

						// Replies
						nat_client.Subscribe("conn_"+worker_nbr, func(m *nats.Msg) {

							global.WorkerNbrs = append(global.WorkerNbrs, worker_nbr)

							atomic.AddInt64(&global.Qps, 1)
							websocket.Message.Send(ws, m.Reply)

							// 角色登录成功后
							ret_json, _ := jason.NewObjectFromBytes([]byte(m.Reply))
							ret_code, _ := ret_json.GetInt64("data", "code")
							log.Debug("ret_code: ", ret_code)
							cmd, _ := ret_json.GetString("cmd")
							if cmd == "user.login" && ret_code == 1 { //&& ret_code==1
								user_sid, _ = ret_json.GetString("data", "sid")
								log.Debug("user_sid: ", user_sid)
								area.WsConnRegister(ws, user_sid)
							}

						})
				*/
				// ws.Write([]byte(ret ))
				// ws.Close()
			}
		}()

	}
	ws.Write([]byte{'E', 'O', 'F'})
	ws.Close()

}