예제 #1
0
파일: main.go 프로젝트: micross/gim
func main() {
	var err error

	fmt.Printf("gim web start....")

	if err = InitConfig(); err != nil {
		fmt.Printf("InitConfig() error(%v)", err)
		return
	}
	// Set max routine
	max := runtime.NumCPU()
	runtime.GOMAXPROCS(max)

	// start http listen.
	StartHTTP()
	// init process
	// sleep one second, let the listen start
	time.Sleep(time.Second)
	// if err = common.InitProcess(Conf.User, Conf.Dir, Conf.PidFile); err != nil {
	// 	fmt.Printf("common.InitProcess() error(%v)", err)
	// 	return
	// }
	// init signals, block wait signals
	signalCH := common.InitSignal()
	common.HandleSignal(signalCH)
	fmt.Printf("gim web stop")
}
예제 #2
0
파일: main.go 프로젝트: micross/gim
func main() {
	var cpuprofile = flag.String("cpuprofile", "", "--cpuprofile=<.prof file path>")
	var memprofile = flag.String("memprofile", "", "--memprofile=<.prof file path>")
	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			panic(err.Error())
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			panic(err.Error())
		}
		pprof.WriteHeapProfile(f)
		defer f.Close()
	}

	fmt.Printf("connect server start\n")

	if err := InitConfig(); err != nil {
		fmt.Printf("InitConfig() error(%v)\n", err)
		return
	}

	// Set max routine
	max := runtime.NumCPU()
	runtime.GOMAXPROCS(max)

	server := CreateServer()
	server.Start()
	defer server.Stop()

	StartRpc()
	// init process
	// sleep one second, let the listen start
	time.Sleep(time.Second)
	// if err := common.InitProcess(Conf.User, Conf.Dir, Conf.PidFile); err != nil {
	// 	fmt.Printf("common.InitProcess() error(%v)\n", err)
	// 	return
	// }
	// init signals, block wait signals
	signalCH := common.InitSignal()
	common.HandleSignal(signalCH)
	fmt.Printf("gim connect server stop\n")
}
예제 #3
0
파일: tcp_client.go 프로젝트: micross/gim
func main() {
	// var uid int
	var ip = "127.0.0.1:8280"
	fmt.Printf("start to connect %s\n", ip)

	conn, err := net.Dial("tcp", ip)
	if err != nil {
		fmt.Printf("connect to %s failed\n", ip)
	}
	fmt.Printf("connect success\n")
	defer conn.Close()

	isOff := 1
	// stdin := bufio.NewReader(os.Stdin)
	stdout := bufio.NewWriter(os.Stdout)
	connin := bufio.NewReader(conn)
	connout := bufio.NewWriter(conn)

	//发起认证
	cc := ClientCmd{
		Cmd:    "AUTH",
		Params: "gim#test#key&gim#test#key#secret&Jack&123456",
	}
	str, _ := json.Marshal(cc)
	connout.WriteString(string(str) + "\n")
	connout.Flush()

	if line, _, err := connin.ReadLine(); err == nil {
		//如果是服务器ping,则需要回复,保持心跳
		var resp Resp
		err = json.Unmarshal(line, &resp)
		if err != nil {
			panic(err.Error())
		}

		if resp.RetCode != 0 {
			fmt.Printf("%s\n", "认证失败")
			return
		}

		if resp.RetCode == 0 && resp.RetType == "AUTH" {
			// uid = int(resp.RetData.(float64))
			fmt.Printf("认证成功\n")
			isOff = 0
		}
	} else {
		return
	}

	//gorouting负责接收服务器消息
	go func() {
		var resp Resp
		for {
			if line, _, err := connin.ReadLine(); err == nil {
				//如果是服务器ping,则需要回复,保持心跳
				err = json.Unmarshal(line, &resp)
				if err != nil {
					fmt.Printf("%v\n", line)
					fmt.Printf("%s\n", err.Error())
				}

				fmt.Printf("%v\n", resp)
				if resp.RetType == "PONG" {
					//保持连接
				} else if resp.RetType == "MSG" {
					msg := resp.RetData.(map[string]interface{})
					mid := int64(msg["Mid"].(float64))
					fmt.Printf("%v\n", mid)
					params := fmt.Sprintf("%d", mid)
					clc := ClientCmd{
						Cmd:    "RACK",
						Params: params,
					}

					str, _ := json.Marshal(clc)
					connout.WriteString(string(str) + "\n")
					connout.Flush()
				} else if resp.RetType == "SACK" {
					fmt.Printf("%v\n", "msg send success")
				} else if resp.RetType == "KICK" {
					fmt.Printf("kick out\n")
					isOff = 1
					break
				}

				// stdout.WriteString(string(line))
				stdout.Flush()
			} else {
				fmt.Printf("read msg failed\n")
				break
			}
		}
	}()

	// 负责接收用户输入
	go func() {
		for {
			if isOff == 1 {
				break
			}

			clc := ClientCmd{
				Cmd:    "MSG",
				Params: "",
			}
			cm := ClientMsg{
				UniqueId: time.Now().UnixNano(),
				Content:  "say hello world from Jack",
				To:       1001,
				Type:     4,
			}
			str, _ := json.Marshal(cm)
			clc.Params = string(str)
			str2, _ := json.Marshal(clc)

			connout.WriteString(string(str2) + "\n")
			connout.Flush()
			fmt.Printf("%v\n", string(str2))
			time.Sleep(4 * time.Second)
		}
	}()

	go func() {
		for {
			if isOff == 1 {
				break
			}
			time.Sleep(time.Second * 3)
			clc := ClientCmd{
				Cmd:    "PING",
				Params: "",
			}
			str, _ := json.Marshal(clc)
			connout.WriteString(string(str) + "\n")
			connout.Flush()
		}
	}()

	signalCH := common.InitSignal()
	common.HandleSignal(signalCH)
	fmt.Printf("client stop\n")
}