Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func handleConnections(conn *websocket.Conn) {
- // Make sure we close the connection when the function returns
- defer conn.Close()
- userName, err := getUsernameFromConn(conn)
- if err != nil {
- log.Fatal(err)
- return
- }
- fmt.Println("Hello *** user ", userName)
- salutionNewUser(userName, broadcast)
- // Register our new client
- addConn(conn, userName)
- // TODO: fix lock
- // if isItNewUser(conn, userName) {
- // fmt.Println("Hello *** user ", userName)
- // salutionNewUser(userName, broadcast)
- // }
- go listeningMsgs(conn, broadcast)
- }
- func listeningMsgs(conn *websocket.Conn, broadcastCh chan Message) {
- for {
- var msg Message
- // Read in a new message as JSON and map it to a Message object
- // err := ws.ReadJSON(&msg) // gorilla
- err := websocket.JSON.Receive(conn, &msg)
- if err != nil {
- log.Printf("error: %v", err)
- removeConn(conn, userName)
- return
- }
- storedUserName, err := getUsernameFromConn(conn)
- if storedUserName != msg.Username {
- log.Fatal("Warning: stored-username and message-author mismatch. Connection will be refused.")
- removeConn(conn, userName)
- return
- }
- // Add connectionId to the message
- mu.Lock()
- msg.ConnID = clients[conn]
- mu.Unlock()
- // Send the newly received message to the broadcast channel
- broadcastCh <- msg
- fmt.Println(msg.Username, "> ", msg.Text)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement