Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
tx7do committed Aug 8, 2022
1 parent 6ee006a commit c728cbb
Show file tree
Hide file tree
Showing 10 changed files with 1,075 additions and 266 deletions.
8 changes: 0 additions & 8 deletions .idea/misc.xml

This file was deleted.

123 changes: 49 additions & 74 deletions api/chatroom/v1/chatroom.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions api/chatroom/v1/chatroom.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ option go_package = "api/chatroom/v1;v1";
service ChatRoom {
}

message WebsocketProto {
string event_id = 1;
string payload = 2;
enum MessageType {
Chat = 0;
}

message ChatMessage {
Expand Down
1 change: 1 addition & 0 deletions app/chatroom/internal/server/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/go-kratos/kratos/contrib/registry/consul/v2"
"github.com/go-kratos/kratos/v2/registry"
consulAPI "github.com/hashicorp/consul/api"

"kratos-chatroom/app/chatroom/internal/conf"
)

Expand Down
23 changes: 20 additions & 3 deletions app/chatroom/internal/server/websocket.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package server

import (
"errors"

"github.com/go-kratos/kratos/v2/encoding"
"github.com/go-kratos/kratos/v2/log"
"github.com/tx7do/kratos-transport/transport/websocket"

v1 "kratos-chatroom/api/chatroom/v1"
"kratos-chatroom/app/chatroom/internal/conf"
"kratos-chatroom/app/chatroom/internal/service"
)

// NewWebsocketServer create a websocket server.
func NewWebsocketServer(c *conf.Server, _ log.Logger, svc *service.ChatRoomService) *websocket.Server {
srv := websocket.NewServer(
websocket.Address(c.Websocket.Addr),
websocket.ReadHandle(c.Websocket.Path, svc.OnWebsocketMessage),
websocket.ConnectHandle(svc.OnWebsocketConnect),
websocket.WithAddress(c.Websocket.Addr),
websocket.WithPath(c.Websocket.Path),
websocket.WithConnectHandle(svc.OnWebsocketConnect),
websocket.WithCodec(encoding.GetCodec("json")),
)

svc.SetWebsocketServer(srv)

srv.RegisterMessageHandler(websocket.MessageType(v1.MessageType_Chat),
func(sessionId websocket.SessionID, payload websocket.MessagePayload) error {
switch t := payload.(type) {
case *v1.ChatMessage:
return svc.OnChatMessage(sessionId, t)
default:
return errors.New("invalid payload type")
}
},
func() websocket.Any { return &v1.ChatMessage{} },
)

return srv
}
53 changes: 6 additions & 47 deletions app/chatroom/internal/service/websocket.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package service

import (
"encoding/json"
"fmt"
"github.com/tx7do/kratos-transport/transport/websocket"
v1 "kratos-chatroom/api/chatroom/v1"
)
Expand All @@ -11,54 +9,15 @@ func (s *ChatRoomService) SetWebsocketServer(ws *websocket.Server) {
s.ws = ws
}

func (s *ChatRoomService) OnWebsocketMessage(connectionId string, message *websocket.Message) error {
s.log.Infof("[%s] Payload: %s\n", connectionId, string(message.Body))

var proto v1.WebsocketProto

if err := json.Unmarshal(message.Body, &proto); err != nil {
s.log.Error("Error unmarshalling proto json %v", err)
return nil
}

switch proto.EventId {
case "chat":
chatMsg := proto.Payload
fmt.Println("chat message:", chatMsg)
_ = s.OnChatMessage(connectionId, &chatMsg)
}

return nil
}

func (s *ChatRoomService) OnChatMessage(connectionId string, msg *string) error {
s.BroadcastToWebsocketClient("chat", msg)
return nil
}

func (s *ChatRoomService) OnWebsocketConnect(connectionId string, register bool) {
func (s *ChatRoomService) OnWebsocketConnect(sessionId websocket.SessionID, register bool) {
if register {
fmt.Printf("%s connected\n", connectionId)
s.log.Infof("%s connected\n", sessionId)
} else {
fmt.Printf("%s disconnect\n", connectionId)
s.log.Infof("%s disconnect\n", sessionId)
}
}

func (s *ChatRoomService) BroadcastToWebsocketClient(eventId string, payload interface{}) {
if payload == nil {
return
}

bufPayload, _ := json.Marshal(&payload)

var proto v1.WebsocketProto
proto.EventId = eventId
proto.Payload = string(bufPayload)

bufProto, _ := json.Marshal(&proto)

var msg websocket.Message
msg.Body = bufProto

s.ws.Broadcast(&msg)
func (s *ChatRoomService) OnChatMessage(sessionId websocket.SessionID, msg *v1.ChatMessage) error {
s.ws.Broadcast(websocket.MessageType(v1.MessageType_Chat), msg)
return nil
}
Loading

0 comments on commit c728cbb

Please sign in to comment.