///============================ ///消费者 获取图片 func getFile(session *link.Session, req map[string]string) error { seq := req["seq"] //死等 vf := QueueInstance.DeChan() if vf == nil { ULogger.Error("getfile time out,sessioninfo is %s", session.State.(*User).Id) ret := map[string]string{ "action": "res_getfile", "seq": seq, } by, _ := json.Marshal(ret) session.Send(link.Bytes(by)) ULogger.Info("send to client", session.Conn().RemoteAddr().String(), "say:", string(by)) return nil } vf.C = session vf.CInfo = session.State.(*User).Id vf.Status = 2 vf.CGetUnix = time.Now().Unix() ret := map[string]string{ "action": "res_getfile", "seq": seq, "id": vf.Id, "file": vf.File, } by, _ := json.Marshal(ret) VFMapInstance.Update("c_getfile", vf) session.Send(link.Bytes(by)) ULogger.Info("send to client", session.Conn().RemoteAddr().String(), "say:", string(by)) return nil }
func BytesTest(t *testing.T, session *link.Session) { for i := 0; i < 2000; i++ { msg1 := RandBytes(512) err := session.Send(msg1) unitest.NotError(t, err) var msg2 []byte err = session.Receive(&msg2) unitest.NotError(t, err) unitest.Pass(t, bytes.Equal(msg1, msg2)) } }
func ObjectTest(t *testing.T, session *link.Session) { for i := 0; i < 2000; i++ { msg1 := RandObject() err := session.Send(&msg1) unitest.NotError(t, err) var msg2 TestObject err = session.Receive(&msg2) unitest.NotError(t, err) unitest.Pass(t, msg1 == msg2) } }
func StringTest(t *testing.T, session *link.Session) { for i := 0; i < 2000; i++ { msg1 := string(RandBytes(512)) err := session.Send(msg1) unitest.NotError(t, err) var msg2 string err = session.Receive(&msg2) unitest.NotError(t, err) unitest.Pass(t, msg1 == msg2) } }
///c端开始答题 func cStart(session *link.Session, req map[string]string) error { userid := req["userid"] password := req["password"] seq := req["seq"] ULogger.Infof("user %s start answer", userid) ret := map[string]string{ "action": "res_cstart", "seq": seq, "result": "0", } if session.State != nil && session.State.(*User).UserType == "C" { ULogger.Error("have logined ", userid) session.Close() } if b := Login(userid, password); !b { ULogger.Errorf("cstart failed ,userid is %s password is %s", userid, password) by, _ := json.Marshal(ret) session.Send(link.Bytes(by)) ULogger.Info("send to client", session.Conn().RemoteAddr().String(), "say:", string(by)) session.Close() return nil } else { user := &User{UserType: "C", Id: userid, WorkTime: time.Now().Format("2006-01-02 15:04:05")} session.State = user ret["result"] = "1" by, _ := json.Marshal(ret) session.Send(link.Bytes(by)) ULogger.Info("send to client", session.Conn().RemoteAddr().String(), "say:", string(by)) //c端开始答题 Exec(`insert into user_activities(user_id,active_time,active_type,user_type,other_info) values(?,now(),'begin','customer',?)`, userid, session.Conn().RemoteAddr().String()) VFMapInstance.AddSession(session) } return nil }
//发送消息 func Send(msgBody []byte, session *link.Session) { session.Send(packet.RAW(msgBody)) }
//发送消息 func Send(session *link.Session, msgBody []byte) { session.Send(msgBody) }