mirror of
https://github.com/jech/galene.git
synced 2024-11-22 16:45:58 +01:00
Allow server to push error message to client.
This commit is contained in:
parent
2cb323ec31
commit
cb1782b6b2
2 changed files with 36 additions and 19 deletions
25
client.go
25
client.go
|
@ -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 {
|
||||||
|
@ -82,6 +91,7 @@ type clientMessage struct {
|
||||||
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"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
Me *bool `json:"me,omitempty"`
|
Me *bool `json:"me,omitempty"`
|
||||||
Offer *webrtc.SessionDescription `json:"offer,omitempty"`
|
Offer *webrtc.SessionDescription `json:"offer,omitempty"`
|
||||||
Answer *webrtc.SessionDescription `json:"answer,omitempty"`
|
Answer *webrtc.SessionDescription `json:"answer,omitempty"`
|
||||||
|
@ -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:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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');
|
||||||
|
|
Loading…
Reference in a new issue