Example #1
0
//  Worker using REQ socket to do load-balancing
//
func hubWorkeDispath(msg []byte, conn *net.TCPConn) {

	//  Process messages as they arrive

	//fmt.Println( "hubWorkeDispath:", string(msg) )

	// 角色登录成功后
	ret_json, _ := jason.NewObjectFromBytes(msg)
	cmd, _ := ret_json.GetString("cmd")

	if cmd == "get_enable" {

		conn.Write([]byte(string(global.AppConfig.Enable)))
		conn.Close()
	}

	if cmd == "enabled" {
		global.AppConfig.Enable = 1
		conn.Write([]byte(string(global.AppConfig.Enable)))
	}

	if cmd == "disabled" {
		global.AppConfig.Enable = 0
		conn.Write([]byte(string(global.AppConfig.Enable)))
	}

	// 创建场景
	if cmd == "create_channel" {

		name, _ := ret_json.GetString("name")
		log.Debug(" join_channel ", name)

		go area.CreateChannel(name, name)
		global.Channels[name] = ""

		conn.Write([]byte(`ok`))
	}

	// 销毁场景
	if cmd == "remove_channel" {

		id, _ := ret_json.GetString("name")
		log.Debug("remove_channel:", id)
		area.RemovChannel(id)
		conn.Write([]byte(`ok`))

	}
	// 加入到一个场景中
	if cmd == "join_channel" {

		sid, _ := ret_json.GetString("sid")
		name, _ := ret_json.GetString("name")
		log.Debug("join_channel:", sid, name)

		// 如果场景不存在,则返回错误
		exist := area.CheckChannelExist(name)
		if !exist {

			conn.Write([]byte(`error,channel not exist`))

		} else {

			// 检查会话用户是否加入过此场景
			have_joined := area.CheckUserJoinChannel(name, sid)

			// 如果还没有加入场景,则订阅
			if !have_joined {

				user_conn := area.GetConn(sid)
				channel_host := global.Channels[name]
				log.Debug(" join_channel ", user_conn, channel_host, sid)
				user_wsconn := area.GetWsConn(sid)

				// 会话如果属于socket
				if user_conn != nil {
					go area.SubscribeChannel(name, user_conn, sid)
				}
				// 会话如果属于websocket
				if user_wsconn != nil {
					go area.SubscribeWsChannel(name, user_wsconn, sid)
				}
				var userJoinedChannels = make([]string, 0, 1000)
				tmp, ok := global.SyncUserJoinedChannels.Get(sid)
				if ok {
					userJoinedChannels = tmp.([]string)
				}
				userJoinedChannels = append(userJoinedChannels, name)
				global.SyncUserJoinedChannels.Set(sid, userJoinedChannels)
			}

			conn.Write([]byte(`ok`))
		}

	}

	if cmd == "leave_channel" {

		sid, _ := ret_json.GetString("sid")
		name, _ := ret_json.GetString("name")
		log.Debug("remove_channel:", sid, name)
		// 离开场景则关闭此订阅
		go area.UnSubscribeChannel(name, sid)

		user_channels, exist := global.UserChannels[sid]
		if exist {
			for i := 0; i < len(user_channels); i++ {
				if user_channels[i] == name {
					user_channels = append(user_channels[:i], user_channels[i+1:]...)
					global.UserChannels[sid] = user_channels
					break
				}
			}
		}

		log.Debug("userChannels's ", sid, ":", global.UserChannels[sid])
		log.Debug("hub_worker leave_channel:", sid, name)
		conn.Write([]byte(`ok`))
	}

	if cmd == "kick" {
		sid, _ := ret_json.GetString("sid")
		user_conn := area.GetConn(sid)
		if user_conn != nil {
			// 发送消息退出
			user_conn.Write([]byte(`{"cmd":"error_","data":{"ret":0,"msg":"Server kicked " }}`))
			user_conn.Close()
			area.DeleteConn(sid)
		}

		user_wsconn := area.GetWsConn(sid)
		if user_wsconn != nil {
			// 发送消息退出
			websocket.Message.Send(user_wsconn, `{"cmd":"error_","data":{"ret":0,"msg":"Server kicked " }}`)
			area.DeleteWsConn(sid)
		}
		area.UserUnSubscribeChannel(sid)
		area.DeleteUserssion(sid)
		conn.Write([]byte(`ok`))
	}
	if cmd == "push" {

		sid, _ := ret_json.GetString("sid")
		push_data, _ := ret_json.GetString("data")

		user_conn := area.GetConn(sid)
		if user_conn != nil {
			user_conn.Write([]byte(fmt.Sprintf("%s\r\n", push_data)))
		}
		user_wsconn := area.GetWsConn(sid)
		if user_wsconn != nil {
			websocket.Message.Send(user_wsconn, fmt.Sprintf("%s\r\n", push_data))
		}

		log.Debug("hub_worker push to  --------------->:", sid, push_data)
		conn.Write([]byte(`ok`))
	}

	if cmd == "broatcast" {

		channel, _ := ret_json.GetString("id")
		data, _ := ret_json.GetString("data")
		area.Broatcast(channel, data)
		log.Debug("hub_worker broadcast to :", channel, "   ", data)
		conn.Write([]byte(`ok`))
	}

	if cmd == "get_channels" {

		js1, _ := json2.Marshal(global.Channels)
		fmt.Println("(global.Channels:", (global.Channels))
		conn.Write(js1)
		conn.Close()
	}

	if cmd == "get_session" {

		sid, _ := ret_json.GetString("sid")
		user_session, exist := global.SyncUserSessions.Get(sid)
		js1 := []byte(`{}`)
		if exist {
			js1, _ = json2.Marshal(user_session)
		}
		conn.Write(js1)
		conn.Close()
	}

	if cmd == "get_all_session" {

		var UserSessions = map[string]*z_type.Session{}
		for item := range global.SyncUserSessions.IterItems() {
			UserSessions[item.Key] = item.Value.(*z_type.Session)
		}
		js1, _ := json2.Marshal(UserSessions)
		conn.Write(js1)
		conn.Close()

	}

	if cmd == "update_session" {

		sid, _ := ret_json.GetString("sid")
		data, _ := ret_json.GetString("data")
		tmp, user_session_exist := global.SyncUserSessions.Get(sid)
		var user_session *z_type.Session
		if user_session_exist {
			user_session = tmp.(*z_type.Session)
			user_session.User = data
			global.SyncUserSessions.Set(sid, user_session)
		}

		log.Info("User Session  :", sid, user_session)
		conn.Write([]byte(`ok`))
	}

	if cmd == "get_user_join_channels" {

		sid, _ := ret_json.GetString("sid")
		js1 := []byte(`[]`)

		tmp, ok := global.SyncUserJoinedChannels.Get(sid)
		if ok {
			userJoinedChannels := tmp.([]string)
			js1, _ = json2.Marshal(userJoinedChannels)

		}
		// 发送消息退出
		conn.Write(js1)
		conn.Close()

	}

}
Example #2
0
func workeDispath(msg string, conn *net.TCPConn, client_idf *string, worker_idf *string) (int, error) {

	//ret_json, _ := jason.NewObjectFromBytes(msg)
	//cmd, _ := ret_json.GetString("cmd")

	ret_arr := strings.Split(msg, "||")
	//fmt.Println("workeDispath ret_arr 4:", ret_arr)
	if len(ret_arr) < 4 {
		//fmt.Println("workeDispath data length error!")
		return 0, nil
	} else {
		if len(ret_arr) > 4 {

			for i := 4; i < int(len(ret_arr)); i++ {

				ret_arr[3] = ret_arr[3] + ret_arr[i]
			}

		}
	}

	cmd := ret_arr[0]
	*client_idf = ret_arr[1]
	*worker_idf = ret_arr[2]
	worker_data := ret_arr[3]
	data := ""

	// 前端连接到代理
	if cmd == global.DATA_REQ_CONNECT {

		//data_byte, _ = protocol.Packet(`{"cmd":"req.connect","ret":200,"msg":"ok"}`)
		data := fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_REQ_CONNECT, "", "", "ok")
		return conn.Write([]byte(data))

	}
	// 前端发数据过来
	if cmd == global.DATA_REQ_MSG {

		//fmt.Println( "workeDispath idf  :", *worker_idf )
		// @todo应该自动分配worker

		worker_conn := GetWorkerConn(*worker_idf)
		log.Info(*worker_idf, worker_conn)
		if worker_conn == nil {
			data = fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_REQ_MSG, *client_idf, *worker_idf, "worker no found!")
			fmt.Println("worker_conn ", *worker_idf, " no found!")
			return conn.Write([]byte(data + "\n"))
		}
		data = fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_REQ_MSG, *client_idf, *worker_idf, worker_data)
		log.Info("workeDispath worker_data :", data)
		return worker_conn.Write([]byte(data + "\n"))
	}
	// worker连接到代理
	if cmd == global.DATA_WORKER_CONNECT {
		AddWorkerConn(*worker_idf, conn)
		data = fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_WORKER_CONNECT, *client_idf, *worker_idf, "ok")
		fmt.Println("worker.connect : ", *worker_idf)
		conn.Write([]byte(data + "\n"))
		go workerPingKick(conn, *worker_idf)

	}
	// worker返回处理结果
	if cmd == global.DATA_WORKER_REPLY {

		//fmt.Println( "worker.reply data 7 :" , ret_arr );
		req_conn := area.GetConn(*client_idf)
		if req_conn == nil {
			fmt.Println("req_conn :", *client_idf, " no found")
		}

		//如果没有找到客户端连接对象则报错并返回
		atomic.AddInt64(&global.Qps, 1)
		if req_conn != nil {
			worker_data_byte, _ := protocol.Packet(worker_data)
			log.Info(worker_data_byte)
			//fmt.Println( "worker.reply  worker_data :" ,  worker_data  );
			return req_conn.Write([]byte(worker_data + "\n"))
		}

		data = fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_WORKER_REPLY, *client_idf, *worker_idf, "req conn no found!")
		return conn.Write([]byte(data + "\n"))
	}

	return 1, nil

}