Commit 7b512962 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Split handshake into login/join.

For now, join must follow login, but it will make it easier to extend
the protocol for joining multiple groups (think federation).
parent 6bde5f98
......@@ -231,6 +231,9 @@ func addClient(name string, c client) (*group, error) {
func delClient(c client) {
g := c.Group()
if g == nil {
return
}
g.mu.Lock()
defer g.mu.Unlock()
......
......@@ -612,12 +612,15 @@ function serverConnect() {
let up = getUserPass();
try {
send({
type: 'handshake',
type: 'login',
id: myid,
group: group,
username: up.username,
password: up.password,
})
send({
type: 'join',
group: group,
})
sendRequest(document.getElementById('requestselect').value);
} catch(e) {
console.error(e);
......
......@@ -619,13 +619,31 @@ func startClient(conn *websocket.Conn) (err error) {
return
}
if m.Type != "handshake" {
if m.Type != "login" {
conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(
websocket.CloseProtocolError,
"you must login first",
),
)
conn.Close()
return
}
if strings.ContainsRune(m.Username, ' ') {
err = userError("don't put spaces in your username")
// at this point, the writer is not running yet, so format
// the message ourselves
conn.WriteJSON(clientMessage{
Type: "error",
Value: "don't put spaces in your username",
})
conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(
websocket.CloseProtocolError,
"don't put spaces in your username",
),
)
conn.Close()
return
}
......@@ -665,6 +683,15 @@ func startClient(conn *websocket.Conn) (err error) {
c.writerDone = make(chan struct{})
go clientWriter(conn, c.writeCh, c.writerDone)
err = conn.ReadJSON(&m)
if err != nil {
return err
}
if m.Type != "join" {
return protocolError("you must join a group first")
}
g, err := addClient(m.Group, c)
if err != nil {
return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment