Beispiel #1
0
func InitZK() (*zk.Conn, error) {
	config := GetConfig()
	if config == nil {
		log.Fatal("get config error")
	}
	conn, err := myzk.Connect(config.Zk.ZookeeperAddr, config.Zk.ZookeeperTimeout)
	if err != nil {
		log.Error("myzk.Connect() error(%v)", err)
		return nil, err
	}

	fpath := path.Join(ZOOKEEPER_GATE_PATH, config.Zk.ZookeeperNode)

	err := myzk.DeleteNode(conn, fpath)
	if err != nil {
		log.Error("myzk delete node error:%v", err)
	}

	data := []byte{}
	log.Debug("myzk node:\"%s\" registe data: \"%s\"", fpath, string(data))
	if err = myzk.RegisterTemp(conn, fpath, data); err != nil {
		log.Error("myzk.RegisterTemp() error(%v)", err)
		return conn, err
	}
	go WatchConnChange(conn)

	return conn, nil
}
Beispiel #2
0
func InitZK() (*zk.Conn, error) {
	config := GetConfig()
	if config == nil {
		log.Fatal("get config error")
	}
	conn, err := myzk.Connect(config.Zk.ZookeeperAddr, config.Zk.ZookeeperTimeout)
	if err != nil {
		log.Error("myzk.Connect() error(%v)", err)
		return nil, err
	}

	fpath := path.Join(ZOOKEEPER_CONN_PATH, config.Zk.ZookeeperNode)

	nodeInfo := ConnectorConf{
		Hostname:  config.Zk.ZookeeperNode,
		ServeAddr: config.ServeAddr,
		RpcAddr:   config.RpcAddr,
	}
	data, err := json.Marshal(nodeInfo)

	if err != nil {
		log.Error("json.Marshal() error(%v)", err)
		return conn, err
	}
	log.Debug("myzk node:\"%s\" registe data: \"%s\"", fpath, string(data))
	err = myzk.DeleteNode(conn, fpath)
	if err != nil {
		log.Error("myzk delete node error:%v", err)
	}
	if err = myzk.RegisterTemp(conn, fpath, data); err != nil {
		log.Error("myzk.RegisterTemp() error(%v)", err)
		return conn, err
	}
	return conn, nil
}
Beispiel #3
0
func rpcServer() {
	rpc.Register(&g_RoomCenter)
	rpc.HandleHTTP()
	l, e := net.Listen("tcp", GetConfig().RpcAddr)
	if e != nil {
		log.Fatal("Listen error:", e)
	}
	http.Serve(l, nil)
}
Beispiel #4
0
func main() {
	flag.Parse()
	loadConfig(false)
	go rpcServer()
	g_zk, _ = InitZK()
	http.Handle("/ws", g_RoomCenter)
	if err := http.ListenAndServe(GetConfig().ServeAddr, nil); err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}
Beispiel #5
0
func main() {
	loadConfig(false)
	rpc.Register(&g_router)
	rpc.HandleHTTP()
	g_zk, _ = InitZK()
	if err := http.ListenAndServe(GetConfig().ServeAddr, nil); err != nil {
		log.Fatal("ListenAndServe error:", err)
	}

}