-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact: make seperation of websocket channel (#52)
seperated websocket channels based on type
- Loading branch information
Showing
5 changed files
with
109 additions
and
48 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
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package models | ||
|
||
import ( | ||
"context" | ||
"github.com/goccy/go-json" | ||
"github.com/mynaparrot/plugNmeet/internal/config" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func DistributeWebsocketMsgToRedisChannel(msg *WebsocketRedisMsg) { | ||
ctx := context.Background() | ||
marshal, err := json.Marshal(msg) | ||
if err != nil { | ||
log.Errorln(err) | ||
return | ||
} | ||
|
||
switch msg.Payload.Type { | ||
case "USER": | ||
config.AppCnf.RDS.Publish(ctx, "plug-n-meet-user-websocket", marshal) | ||
case "WHITEBOARD": | ||
config.AppCnf.RDS.Publish(ctx, "plug-n-meet-whiteboard-websocket", marshal) | ||
case "SYSTEM": | ||
config.AppCnf.RDS.Publish(ctx, "plug-n-meet-system-websocket", marshal) | ||
} | ||
} | ||
|
||
// SubscribeToUserWebsocketChannel will delivery message to user websocket | ||
func SubscribeToUserWebsocketChannel() { | ||
ctx := context.Background() | ||
pubsub := config.AppCnf.RDS.Subscribe(ctx, "plug-n-meet-user-websocket") | ||
defer pubsub.Close() | ||
|
||
_, err := pubsub.Receive(ctx) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
m := NewWebsocketService() | ||
ch := pubsub.Channel() | ||
for msg := range ch { | ||
res := new(WebsocketRedisMsg) | ||
err = json.Unmarshal([]byte(msg.Payload), res) | ||
if err != nil { | ||
log.Errorln(err) | ||
} | ||
if res.Type == "sendMsg" { | ||
m.HandleDataMessages(res.Payload, res.RoomId, res.IsAdmin) | ||
} else if res.Type == "deleteRoom" { | ||
config.AppCnf.DeleteChatRoom(res.RoomId) | ||
} | ||
} | ||
} | ||
|
||
// SubscribeToWhiteboardWebsocketChannel will delivery message to whiteboard websocket | ||
func SubscribeToWhiteboardWebsocketChannel() { | ||
ctx := context.Background() | ||
pubsub := config.AppCnf.RDS.Subscribe(ctx, "plug-n-meet-whiteboard-websocket") | ||
defer pubsub.Close() | ||
|
||
_, err := pubsub.Receive(ctx) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
m := NewWebsocketService() | ||
ch := pubsub.Channel() | ||
for msg := range ch { | ||
res := new(WebsocketRedisMsg) | ||
err = json.Unmarshal([]byte(msg.Payload), res) | ||
if err != nil { | ||
log.Errorln(err) | ||
} | ||
m.HandleDataMessages(res.Payload, res.RoomId, res.IsAdmin) | ||
} | ||
} | ||
|
||
// SubscribeToSystemWebsocketChannel will delivery message to websocket | ||
func SubscribeToSystemWebsocketChannel() { | ||
ctx := context.Background() | ||
pubsub := config.AppCnf.RDS.Subscribe(ctx, "plug-n-meet-system-websocket") | ||
defer pubsub.Close() | ||
|
||
_, err := pubsub.Receive(ctx) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
m := NewWebsocketService() | ||
ch := pubsub.Channel() | ||
for msg := range ch { | ||
res := new(WebsocketRedisMsg) | ||
err = json.Unmarshal([]byte(msg.Payload), res) | ||
if err != nil { | ||
log.Errorln(err) | ||
} | ||
m.HandleDataMessages(res.Payload, res.RoomId, res.IsAdmin) | ||
} | ||
} |