func CreateHandler(user *User, data []byte) { // request body unmarshaling req := new(gs_protocol.ReqCreate) err := proto.Unmarshal(data, req) gs.CheckError(err) if user.userID != req.GetUserID() { if DEBUG { gs.Log("Fail room create, user id missmatch") } return } // room create roomID := GetRandomRoomID() r := NewRoom(roomID) r.users.Set(user.userID, user) // insert user user.room = r // set room rooms.Set(roomID, r) // set room into global shared map if DEBUG { gs.Log("Get rand room id : ", gs.Itoa64(roomID)) } // response body marshaling res := new(gs_protocol.ResCreate) res.RoomID = proto.Int64(roomID) res.UserID = proto.Int64(user.userID) if DEBUG { gs.Log("Room create, room id : ", gs.Itoa64(roomID)) } msg, err := proto.Marshal(res) gs.CheckError(err) user.Push(NewMessage(user.userID, gs_protocol.Type_Create, msg)) }
func ClientSender(user *User, c net.Conn) { defer user.Leave() for { select { case <-user.exit: // when receive signal then finish the program if DEBUG { gs.Log("Leave user id :" + gs.Itoa64(user.userID)) } return case m := <-user.recv: // on receive message msgTypeBytes := gs.WriteMsgType(m.msgType) // msg header + msg type msg := append(msgTypeBytes, m.content...) // '...' need when concat between slice+slice if DEBUG { gs.Log("Client recv, user id : " + gs.Itoa64(user.userID)) } _, err := c.Write(msg) // send data to client if err != nil { if DEBUG { gs.Log(err) } return } } } }
func Action1Handler(user *User, data []byte) { // request body unmarshaling req := new(gs_protocol.ReqAction1) err := proto.Unmarshal(data, req) gs.CheckError(err) // TODO create business logic for Action1 Type if DEBUG { gs.Log("Action1 userID : ", gs.Itoa64(req.GetUserID())) } // broadcast message notifyMsg := new(gs_protocol.NotifyAction1Msg) notifyMsg.UserID = proto.Int64(user.userID) msg, err := proto.Marshal(notifyMsg) gs.CheckError(err) user.SendToAll(NewMessage(user.userID, gs_protocol.Type_NotifyAction1, msg)) // response body marshaling res := new(gs_protocol.ResAction1) res.UserID = proto.Int64(user.userID) res.Result = proto.Int32(1) // is success? msg, err = proto.Marshal(res) gs.CheckError(err) user.Push(NewMessage(user.userID, gs_protocol.Type_DefinedAction1, msg)) }
func (r *Room) RoomMessageLoop() { // when messages channel is closed then "for-loop" will be break for m := range r.messages { for userID, _ := range r.users.Map() { if userID != m.userID { value, ok := r.users.Get(userID) if ok { user := value.(*User) if DEBUG { gs.Log("Push message for broadcast :" + gs.Itoa64(user.userID)) } user.Push(m) } } } } }
func JoinHandler(user *User, data []byte) { // request body unmarshaling req := new(gs_protocol.ReqJoin) err := proto.Unmarshal(data, req) gs.CheckError(err) roomID := req.GetRoomID() value, ok := rooms.Get(roomID) if !ok { if DEBUG { gs.Log("Fail room join, room does not exist, room id : ", gs.Itoa64(roomID)) } return } r := value.(*Room) r.users.Set(user.userID, user) user.room = r // broadcast message notifyMsg := new(gs_protocol.NotifyJoinMsg) notifyMsg.UserID = proto.Int64(user.userID) notifyMsg.RoomID = proto.Int64(roomID) msg, err := proto.Marshal(notifyMsg) gs.CheckError(err) user.SendToAll(NewMessage(user.userID, gs_protocol.Type_NotifyJoin, msg)) // response body marshaling res := new(gs_protocol.ResJoin) res.UserID = proto.Int64(user.userID) res.RoomID = proto.Int64(roomID) res.Members = r.getRoomUsers() msg, err = proto.Marshal(res) gs.CheckError(err) user.Push(NewMessage(user.userID, gs_protocol.Type_Join, msg)) }