示例#1
0
/**
 * zeromore 框架启动
 */
func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())

	go func() {
		log2.Println(http.ListenAndServe("0.0.0.0:6060", nil))
	}()

	global.InitConfig()

	//fmt.Println( global.Config.Connector.MaxConections )
	//fmt.Println( global.Config.Area.Init_area )
	//fmt.Println( global.Config.WorkerAgent.Host )
	//hub.CronTest()
	//return
	//appConfig := &AppConfig{}
	// 读取配置文件

	init_logger()
	init_global()
	go connector.Websocket_connector("", global.Config.Connector.SocketPort)
	go connector.Keeplive_connector("", global.Config.Connector.WebsocketPort)

	// 开启worker代理
	go worker.Worker_agent()

	// 开启hub服务器
	go hub.HubServer()

	// 预创建多个场景
	for _, area_id := range global.Config.Area.Init_area {
		area.CreateChannel(area_id, area_id)
		global.Channels[area_id] = global.Config.Hub.Hub_host
	}

	// 启动worker
	//go start_php_worker()
	//go worker.Start()
	log.Info("Server started!")

	select {}

}
示例#2
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()

	}

}
示例#3
0
func CreateChannel(id string, name string) bool {

	area.CreateChannel(id, name)
	return true
}