예제 #1
0
파일: main.go 프로젝트: kyugao/gametcp_ex
func (this *Callback) OnMessage(c *gametcp.Conn, p protocol.Packet) bool {
	packet := &p

	reqContent := packet.GetBody()
	msgId := packet.GetMsgId()
	var obj interface{}
	json.Unmarshal(reqContent, &obj)
	objparams := obj.(map[string]interface{})
	var params map[string]string
	params = make(map[string]string)
	for k, v := range objparams {
		params[k] = v.(string)
	}
	methodName := actList[msgId]

	var response map[string]string
	response = make(map[string]string)

	var rid string

	if methodName != "Login" && methodName != "Signup" {
		//verify
		verifyInfo, err := controllers.Verify(params["verify"])
		if err != nil {
			response["status"] = "fail"
			response["msg"] = err.Error()
		} else {
			reflectData, err := funcs.Call(methodName, params)
			if err != nil {
				logger.PutLog(err.Error(), logdir, "error")
			}
			i := reflectData[0].Interface()
			response = i.(map[string]string)
		}
		rid = verifyInfo["rid"]
	} else {
		reflectData, err := funcs.Call(methodName, params)
		if err != nil {
			logger.PutLog(err.Error(), logdir, "error")
		}
		i := reflectData[0].Interface()
		response = i.(map[string]string)
		rid = response["rid"]
	}
	rspBytes, _ := json.Marshal(response)
	rspPacket := protocol.NewPacket(rspBytes, msgId, false)
	c.AsyncWritePacket(rspPacket, time.Second)
	logmsg := rid + " Req: " + string(reqContent) + " Rsp: " + string(rspBytes)
	logger.PutLog(logmsg, logdir, methodName)
	return true
}
예제 #2
0
파일: main.go 프로젝트: kyugao/gametcp_ex
func (this *Callback) OnConnect(c *gametcp.Conn) bool {
	addr := c.GetRawConn().RemoteAddr()
	c.PutExtraData(addr)
	logger.PutLog("OnConnect:"+addr.String(), logdir, "info")
	return true
}
예제 #3
0
파일: main.go 프로젝트: kyugao/gametcp_ex
func (this *Callback) OnClose(c *gametcp.Conn) {
	logger.PutLog("OnClose:"+c.GetExtraData().(net.Addr).String(), logdir, "info")
}