R
R
Riedel872016-02-02 08:14:22
go
Riedel87, 2016-02-02 08:14:22

How to not send message to websocket sender in GO language?

Hello!
Here I took an example for creating a web socket (link below to github) and sending a message to all devices that are connected to the server.
The server sends everything well, but it sends a message even to the one who sent this message, but this is not necessary.
I can't figure out how to extract the sender and not send him a message of his own.
Can you help? Google has searched all over already :(
There is a sendall function, it is responsible for sending, apparently it needs to be corrected somehow...
https://github.com/jakecoffman/go-angular-tutorial...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2016-02-02
@Riedel87

I guess you need it

func sendAll(msg []byte, myconn *websocket.Conn) {
    for conn := range connections {
        if myconn != conn {
            if err := conn.WriteMessage(websocket.TextMessage, msg); err != nil {
                delete(connections, conn)
                conn.Close()
            }
        }
    }
}

PS And you are changing the map from several threads at the same time, this is very bad. One day your application will crash due to a memory error. If you want to change global objects from several goroutines (and net / http handles each connection in a separate goroutine), then you need to ensure that only one goroutine works with the map at a time. This is done through mutexes, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question