1
Fork 0

Allow server to push error message to client.

This commit is contained in:
Juliusz Chroboczek 2020-04-25 02:09:11 +02:00
parent 2cb323ec31
commit cb1782b6b2
2 changed files with 36 additions and 19 deletions

View File

@ -54,7 +54,13 @@ func (err protocolError) Error() string {
return string(err) return string(err)
} }
func errorToWSCloseMessage(err error) []byte { type userError string
func (err userError) Error() string {
return string(err)
}
func errorToWSCloseMessage(err error) (string, []byte) {
var code int var code int
var text string var text string
switch e := err.(type) { switch e := err.(type) {
@ -63,10 +69,13 @@ func errorToWSCloseMessage(err error) []byte {
case protocolError: case protocolError:
code = websocket.CloseProtocolError code = websocket.CloseProtocolError
text = string(e) text = string(e)
case userError:
code = websocket.CloseNormalClosure
text = string(e)
default: default:
code = websocket.CloseInternalServerErr code = websocket.CloseInternalServerErr
} }
return websocket.FormatCloseMessage(code, text) return "The server said: " + text, websocket.FormatCloseMessage(code, text)
} }
func isWSNormalError(err error) bool { func isWSNormalError(err error) bool {
@ -76,19 +85,20 @@ func isWSNormalError(err error) bool {
} }
type clientMessage struct { type clientMessage struct {
Type string `json:"type"` Type string `json:"type"`
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
Group string `json:"group,omitempty"` Group string `json:"group,omitempty"`
Value string `json:"value,omitempty"` Value string `json:"value,omitempty"`
Me *bool `json:"me,omitempty"` Message string `json:"message,omitempty"`
Offer *webrtc.SessionDescription `json:"offer,omitempty"` Me *bool `json:"me,omitempty"`
Answer *webrtc.SessionDescription `json:"answer,omitempty"` Offer *webrtc.SessionDescription `json:"offer,omitempty"`
Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"` Answer *webrtc.SessionDescription `json:"answer,omitempty"`
Del bool `json:"del,omitempty"` Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"`
AudioRate int `json:"audiorate,omitempty"` Del bool `json:"del,omitempty"`
VideoRate int `json:"audiorate,omitempty"` AudioRate int `json:"audiorate,omitempty"`
VideoRate int `json:"audiorate,omitempty"`
} }
type closeMessage struct { type closeMessage struct {
@ -120,10 +130,15 @@ func startClient(conn *websocket.Conn) (err error) {
if isWSNormalError(err) { if isWSNormalError(err) {
err = nil err = nil
} else { } else {
m, e := errorToWSCloseMessage(err)
if m != "" {
c.write(clientMessage{
Type: "error",
Message: m,
})
}
select { select {
case c.writeCh <- closeMessage{ case c.writeCh <- closeMessage{e}:
errorToWSCloseMessage(err),
}:
case <-c.writerDone: case <-c.writerDone:
} }
} }

View File

@ -311,6 +311,9 @@ function serverConnect() {
case 'chat': case 'chat':
addToChatbox(m.id, m.username, m.value, m.me); addToChatbox(m.id, m.username, m.value, m.me);
break; break;
case 'error':
displayError(m.message);
break;
default: default:
console.warn('Unexpected server message', m.type); console.warn('Unexpected server message', m.type);
return; return;
@ -773,7 +776,6 @@ document.getElementById('disconnectbutton').onclick = function(e) {
socket.close(); socket.close();
} }
function start() { function start() {
group = decodeURIComponent(location.pathname.replace(/^\/[a-z]*\//, '')); group = decodeURIComponent(location.pathname.replace(/^\/[a-z]*\//, ''));
let title = document.getElementById('title'); let title = document.getElementById('title');