Fix graceful disconnect commands from the server

polling-api
Noah 2023-10-23 19:05:02 -07:00
parent 95c6c7859f
commit 2ac3e8e128
4 changed files with 14 additions and 6 deletions

View File

@ -152,7 +152,8 @@ func (s *Server) KickCommand(words []string, sub *Subscriber) {
other.SendJSON(messages.Message{ other.SendJSON(messages.Message{
Action: messages.ActionKick, Action: messages.ActionKick,
}) })
s.DeleteSubscriber(other) other.authenticated = false
other.Username = ""
sub.ChatServer("%s has been kicked from the room", username) sub.ChatServer("%s has been kicked from the room", username)
// Broadcast it to everyone. // Broadcast it to everyone.
@ -200,7 +201,8 @@ func (s *Server) KickAllCommand() {
continue continue
} }
s.DeleteSubscriber(sub) sub.authenticated = false
sub.Username = ""
} }
} }
@ -245,7 +247,8 @@ func (s *Server) BanCommand(words []string, sub *Subscriber) {
other.SendJSON(messages.Message{ other.SendJSON(messages.Message{
Action: messages.ActionKick, Action: messages.ActionKick,
}) })
s.DeleteSubscriber(other) other.authenticated = false
other.Username = ""
sub.ChatServer("%s has been banned from the room for %d hours.", username, duration/time.Hour) sub.ChatServer("%s has been banned from the room for %d hours.", username, duration/time.Hour)
} }
} }

View File

@ -62,7 +62,8 @@ func (s *Server) OnLogin(sub *Subscriber, msg messages.Message) {
other.SendJSON(messages.Message{ other.SendJSON(messages.Message{
Action: messages.ActionKick, Action: messages.ActionKick,
}) })
s.DeleteSubscriber(other) other.authenticated = false
other.Username = ""
} }
// They will take over their original username. // They will take over their original username.

View File

@ -229,7 +229,7 @@ func (s *Server) WebSocket() http.HandlerFunc {
var token string var token string
if sub.JWTClaims != nil { if sub.JWTClaims != nil {
if jwt, err := sub.JWTClaims.ReSign(); err != nil { if jwt, err := sub.JWTClaims.ReSign(); err != nil {
log.Error("ReSign JWT token for %s: %s", sub.Username, err) log.Error("ReSign JWT token for %s#%d: %s", sub.Username, sub.ID, err)
} else { } else {
token = jwt token = jwt
} }
@ -279,6 +279,7 @@ func (s *Server) DeleteSubscriber(sub *Subscriber) {
// Cancel its context to clean up the for-loop goroutine. // Cancel its context to clean up the for-loop goroutine.
if sub.cancel != nil { if sub.cancel != nil {
log.Info("Calling sub.cancel() on subscriber: %s#%d", sub.Username, sub.ID)
sub.cancel() sub.cancel()
} }

View File

@ -1283,7 +1283,7 @@ export default {
} }
if (!this.disconnect) { if (!this.disconnect) {
if (ev.code !== 1001) { if (ev.code !== 1001 && ev.code !== 1000) {
this.ChatClient("Reconnecting in 5s"); this.ChatClient("Reconnecting in 5s");
setTimeout(this.dial, 5000); setTimeout(this.dial, 5000);
} }
@ -1376,7 +1376,10 @@ export default {
}); });
break; break;
case "disconnect": case "disconnect":
this.onWho({ whoList: [] });
this.disconnect = true; this.disconnect = true;
this.ws.connected = false;
this.ws.conn.close(1000, "server asked to close the connection");
break; break;
case "ping": case "ping":
// New JWT token? // New JWT token?