-
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.
Merge pull request #4 from satanaroom/task6
Task6
- Loading branch information
Showing
32 changed files
with
1,912 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,9 @@ go.work | |
/.idea | ||
/vendor.protogen | ||
.env | ||
ca.key | ||
ca.cert | ||
certificate.conf | ||
service.csr | ||
service.key | ||
service.pem |
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,51 @@ | ||
package chat_v1 | ||
|
||
import ( | ||
"github.com/satanaroom/chat_server/internal/sys" | ||
"github.com/satanaroom/chat_server/internal/sys/codes" | ||
desc "github.com/satanaroom/chat_server/pkg/chat_v1" | ||
) | ||
|
||
func (i *Implementation) ConnectChat(req *desc.ConnectChatRequest, stream desc.ChatV1_ConnectChatServer) error { | ||
chatId := req.GetChatId() | ||
username := req.GetUsername() | ||
|
||
chatChannel, err := i.chatService.GetChatChannel(chatId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
i.chats.mu.Lock() | ||
if _, ok := i.chats.m[chatId]; !ok { | ||
i.chats.m[chatId] = &Chat{ | ||
streams: make(map[string]desc.ChatV1_ConnectChatServer), | ||
} | ||
} | ||
i.chats.mu.Unlock() | ||
|
||
i.chats.m[chatId].mu.Lock() | ||
i.chats.m[chatId].streams[username] = stream | ||
i.chats.m[chatId].mu.Unlock() | ||
|
||
for { | ||
select { | ||
case msg, ok := <-chatChannel: | ||
if !ok { | ||
return nil | ||
} | ||
|
||
for _, str := range i.chats.m[chatId].streams { | ||
if err = str.Send(&desc.ConnectChatResponse{ | ||
Message: msg, | ||
}); err != nil { | ||
return sys.NewCommonError("failed to send message", codes.Internal) | ||
} | ||
} | ||
case <-stream.Context().Done(): | ||
i.chats.m[chatId].mu.Lock() | ||
delete(i.chats.m[chatId].streams, username) | ||
i.chats.m[chatId].mu.Unlock() | ||
return nil | ||
} | ||
} | ||
} |
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,19 @@ | ||
package chat_v1 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
desc "github.com/satanaroom/chat_server/pkg/chat_v1" | ||
) | ||
|
||
func (i *Implementation) SendMessage(_ context.Context, req *desc.SendMessageRequest) (*empty.Empty, error) { | ||
chatChannel, err := i.chatService.GetChatChannel(req.GetChatId()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
chatChannel <- req.GetMessage() | ||
|
||
return &empty.Empty{}, nil | ||
} |
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.