Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socker message recieve chat id and sender id added #37

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions server/pkg/api/service/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type webSocketService struct {
}

type Message struct {
SenderID uint `json:"sender_id"` // will remove if its not necessary
ChatID uint `json:"chat_id"`
ReceiverID uint `json:"receiver_id"`
Content string `json:"content"`
}
Expand All @@ -52,30 +54,30 @@ func NewWebSocketService(tokenService token.TokenService) WebSocketService {
// @Tags Users Socket
// @Router /ws [get]
func (c *webSocketService) ServeWebSocket(ctx *gin.Context) {
soketConn, err := c.upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
socketConn, err := c.upgrader.Upgrade(ctx.Writer, ctx.Request, nil)

if err != nil {
log.Println("failed to upgrade connection")
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}

closeHand := soketConn.CloseHandler()
closeHand := socketConn.CloseHandler()

userID, err := c.verifyConnection(ctx, soketConn)
userID, err := c.verifyConnection(ctx, socketConn)
if err != nil {
log.Println("failed to verify token", err)
closeHand(websocket.ClosePolicyViolation, err.Error())
return
}

c.mu.Lock()
c.connections[userID] = soketConn
c.connections[userID] = socketConn
c.mu.Unlock()

{ // wait until the connectin close
{ // wait until the connection close
fmt.Println("successfully connected for user_id : ", userID)
go c.readMessages(ctx, soketConn) // read messages
go c.readMessages(ctx, socketConn) // read messages
<-ctx.Done()
log.Println("connection closed from request")
}
Expand All @@ -94,7 +96,7 @@ func (c *webSocketService) verifyConnection(ctx context.Context, sc *websocket.C
tokenChan := make(chan TokenRequest)
errChan := make(chan error)

// wait for the token to send within 5 second of connectin established
// wait for the token to send within 5 second of connection established
go func() {
var body TokenRequest
err := sc.ReadJSON(&body)
Expand Down Expand Up @@ -134,30 +136,30 @@ func (c *webSocketService) readMessages(ctx context.Context, sc *websocket.Conn)

}()

// get each messge and if the whole connectin lost then return
// get each message and if the whole connection lost then return
for {
select {
case message := <-messageChan:
go c.sendMessge(message)
go c.sendMessage(message)
case <-ctx.Done():
return
}
}
}

func (c *webSocketService) sendMessge(messge Message) (received bool, err error) {
func (c *webSocketService) sendMessage(message Message) (received bool, err error) {

c.mu.Lock()
defer c.mu.Unlock()
// find other connection
conn, ok := c.connections[messge.ReceiverID]
conn, ok := c.connections[message.ReceiverID]
if !ok {
return false, nil
}

// send to other connection
if err := conn.WriteJSON(messge); err != nil {
log.Println("failed to write message: ", messge)
if err := conn.WriteJSON(message); err != nil {
log.Println("failed to write message: ", message)
return false, err
}
return true, nil
Expand Down
Loading