-
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.
The chat API enables users to chat with each other in a globally available chat. It supports basic text messages to be sent between each user.
- Loading branch information
Showing
14 changed files
with
398 additions
and
16 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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
!cmd/ | ||
!pkg/ | ||
!pkg/api/ | ||
!pkg/chat/ | ||
!pkg/filesystem/ | ||
!pkg/game/ | ||
!pkg/game/argument/ | ||
|
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,98 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/seternate/go-lanty/pkg/chat" | ||
) | ||
|
||
type ChatService struct { | ||
client *Client | ||
ws *websocket.Conn | ||
|
||
Messages chan chat.Message | ||
Error error | ||
} | ||
|
||
// If client.baseURL is changed while being connected a Reconnect for this service has to be made | ||
func (service *ChatService) Connect() (resp *http.Response, err error) { | ||
defer func() { service.Error = err }() | ||
path, err := service.client.router.Get("Chat").URLPath() | ||
if err != nil { | ||
return | ||
} | ||
path.Scheme = "ws" | ||
url := service.client.buildURL(*path) | ||
service.ws, resp, err = websocket.DefaultDialer.Dial((&url).String(), nil) | ||
if err != nil { | ||
err = fmt.Errorf("error connecting to server chat websocket: %w", err) | ||
return | ||
} | ||
service.Error = nil | ||
go service.run() | ||
return | ||
} | ||
|
||
func (service *ChatService) Disconnect() (err error) { | ||
if service.ws == nil { | ||
return | ||
} | ||
err = service.ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) | ||
if err != nil { | ||
service.Error = err | ||
} | ||
return | ||
} | ||
|
||
func (service *ChatService) Reconnect() (err error) { | ||
defer func() { service.Error = err }() | ||
err = service.Disconnect() | ||
_, err = service.Connect() | ||
return | ||
} | ||
|
||
func (service *ChatService) SendMessage(message chat.Message) (err error) { | ||
if service.ws == nil { | ||
return fmt.Errorf("can not send chat message: %w", service.Error) | ||
} | ||
message.SetTime(time.Now()) | ||
err = service.ws.WriteJSON(message) | ||
if err != nil { | ||
service.Error = err | ||
} | ||
return | ||
} | ||
|
||
func (service *ChatService) run() { | ||
for service.Error == nil && service.ws != nil { | ||
messageType, reader, err := service.ws.NextReader() | ||
if err != nil { | ||
service.Error = fmt.Errorf("%w: permanent error reading from client websocket", err) | ||
service.Disconnect() | ||
return | ||
} | ||
if messageType == websocket.BinaryMessage { | ||
continue | ||
} | ||
|
||
message, err := chat.ReadMessage(reader) | ||
if err != nil { | ||
continue | ||
} | ||
if len(strings.TrimSpace(message.GetMessage())) == 0 { | ||
continue | ||
} | ||
if len(strings.TrimSpace(message.GetUser().Name)) == 0 || len(strings.TrimSpace(message.GetUser().IP)) == 0 { | ||
continue | ||
} | ||
if message.GetTime().IsZero() { | ||
continue | ||
} | ||
|
||
service.Messages <- message | ||
} | ||
} |
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,49 @@ | ||
package chat | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"github.com/seternate/go-lanty/pkg/user" | ||
) | ||
|
||
type Message interface { | ||
GetType() Type | ||
GetUser() user.User | ||
GetMessage() string | ||
GetTime() time.Time | ||
SetTime(time.Time) | ||
} | ||
|
||
type tmpMessage struct { | ||
Type Type `json:"type"` | ||
User user.User `json:"user"` | ||
Time time.Time `json:"time"` | ||
} | ||
|
||
func ReadMessage(r io.Reader) (Message, error) { | ||
tmpMessageJson, err := io.ReadAll(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tmpMessage := &tmpMessage{} | ||
err = json.Unmarshal(tmpMessageJson, tmpMessage) | ||
if err == io.EOF { | ||
err = io.ErrUnexpectedEOF | ||
return nil, err | ||
} | ||
|
||
switch tmpMessage.Type { | ||
case TYPE_TEXT: | ||
textmessage := &TextMessage{} | ||
err = json.Unmarshal(tmpMessageJson, textmessage) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return textmessage, err | ||
} | ||
return nil, fmt.Errorf("undefined message type: %s", tmpMessage.Type.String()) | ||
} |
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,47 @@ | ||
package chat | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/seternate/go-lanty/pkg/user" | ||
) | ||
|
||
type TextMessage struct { | ||
Type Type `json:"type"` | ||
User user.User `json:"user"` | ||
Message textMessageText `json:"message"` | ||
Time time.Time `json:"time"` | ||
} | ||
|
||
type textMessageText struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
func NewTextMessage(user user.User, message string) *TextMessage { | ||
return &TextMessage{ | ||
Type: TYPE_TEXT, | ||
User: user, | ||
Message: textMessageText{message}, | ||
Time: time.Now(), | ||
} | ||
} | ||
|
||
func (message *TextMessage) GetType() Type { | ||
return message.Type | ||
} | ||
|
||
func (message *TextMessage) GetUser() user.User { | ||
return message.User | ||
} | ||
|
||
func (message *TextMessage) GetMessage() string { | ||
return message.Message.Text | ||
} | ||
|
||
func (message *TextMessage) GetTime() time.Time { | ||
return message.Time | ||
} | ||
|
||
func (message *TextMessage) SetTime(t time.Time) { | ||
message.Time = t | ||
} |
Oops, something went wrong.