forked from lonng/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster,example: fix chat cluster demo
Signed-off-by: Lonng <[email protected]>
- Loading branch information
Showing
12 changed files
with
202 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,60 @@ | ||
package chat | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/lonng/nano" | ||
"github.com/lonng/nano/component" | ||
"github.com/lonng/nano/examples/cluster/protocol" | ||
"github.com/lonng/nano/session" | ||
"github.com/pingcap/errors" | ||
) | ||
|
||
type RoomService struct { | ||
component.Base | ||
group *nano.Group | ||
} | ||
|
||
func newRoomService() *RoomService { | ||
return &RoomService{} | ||
return &RoomService{ | ||
group: nano.NewGroup("all-users"), | ||
} | ||
} | ||
|
||
func (rs *RoomService) JoinRoom(s *session.Session, msg *protocol.JoinRoomRequest) error { | ||
if err := s.Bind(msg.MasterUid); err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
broadcast := &protocol.NewUserBroadcast{ | ||
Content: fmt.Sprintf("User user join: %v", msg.Nickname), | ||
} | ||
if err := rs.group.Broadcast("onNewUser", broadcast); err != nil { | ||
return errors.Trace(err) | ||
} | ||
return rs.group.Add(s) | ||
} | ||
|
||
type SyncMessage struct { | ||
Name string `json:"name"` | ||
Content string `json:"content"` | ||
} | ||
|
||
func (rs *RoomService) SyncMessage(s *session.Session, msg *SyncMessage) error { | ||
// Send an RPC to master server to stats | ||
if err := s.RPC("TopicService.Stats", &protocol.MasterStats{Uid: s.UID()}); err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
// Sync message to all members in this room | ||
return rs.group.Broadcast("onMessage", msg) | ||
} | ||
|
||
func (cs *RoomService) JoinTopic(s *session.Session, msg []byte) error { | ||
return errors.Errorf("not implement") | ||
func (rs *RoomService) userDisconnected(s *session.Session) { | ||
if err := rs.group.Leave(s); err != nil { | ||
log.Println("Remove user from group failed", s.UID(), err) | ||
return | ||
} | ||
log.Println("User session disconnected", s.UID()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.