diff --git a/database.go b/database.go index b2c612c..cca7171 100644 --- a/database.go +++ b/database.go @@ -57,7 +57,7 @@ func AddNewUser(ctx context.Context, user User) (uint32, error) { INSERT INTO users (Name, PassHash, Color) VALUES ($1, $2, $3) RETURNING Id - `, user.Name, user.IsPasswordHashed, user.Color).Scan(&id) + `, user.Name, user.Password, user.Color).Scan(&id) return id, err } diff --git a/go-socket b/go-socket index 9b84457..13b2a7a 100755 Binary files a/go-socket and b/go-socket differ diff --git a/http.go b/http.go index 57304ab..2f10170 100644 --- a/http.go +++ b/http.go @@ -1,6 +1,7 @@ package main import ( + "log" "net/http" "golang.org/x/crypto/bcrypt" @@ -30,6 +31,14 @@ func RegisterHandler(response http.ResponseWriter, request *http.Request) { } if _, err := AddNewUser(ctx, User{0, username, password, "xxx", false}); err != nil { + http.Error(response, "Internal server error", http.StatusInternalServerError) + log.Fatal(err) + return + } + + response.WriteHeader(http.StatusCreated) + _, err := response.Write([]byte("registered")) + if err != nil { http.Error(response, "Internal server error", http.StatusInternalServerError) return } diff --git a/machine-client/index.html b/machine-client/index.html new file mode 100644 index 0000000..01cdde3 --- /dev/null +++ b/machine-client/index.html @@ -0,0 +1,151 @@ + + + + + WS Client + + + + +
+

WebSocket Client

+ + + +
Not connected
+ +
+ Register: curl -X POST localhost:8080/register -d "username=alice&password=password123"

+ Login: curl -X POST localhost:8080/login -d "username=alice&password=password123" +
+
+ +
+

Connected as

+
+
+ + +
+ +
+ + + + diff --git a/wsServer.go b/wsServer.go index dffdd00..6ed9bc8 100644 --- a/wsServer.go +++ b/wsServer.go @@ -102,6 +102,16 @@ func sendAndCloseIfFails(conn *websocket.Conn, message map[string]any) { } } +func sendToAllExceptAndCloseIfFails(conn *websocket.Conn, message map[string]any) { + _, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + for _, aConn := range authenticatedConnections { + if aConn.connection != conn { + sendAndCloseIfFails(aConn.connection, message) + } + } +} + func handleUnauthenticatedMessage(ctx context.Context, conn *websocket.Conn, msg map[string]any) { token := msg["token"].(string) subject, err := GetSubject(token) @@ -138,5 +148,11 @@ func handleAuthenticatedMessage(conn *websocket.Conn, msg map[string]any) { sendAndCloseIfFails(conn, map[string]any{ "error": "no message", }) + return } + + sendToAllExceptAndCloseIfFails(conn, map[string]any{ + "username": , + "message": message, + }) }