Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat example. #95

Closed
mistercorea opened this issue Nov 6, 2015 · 16 comments
Closed

Chat example. #95

mistercorea opened this issue Nov 6, 2015 · 16 comments

Comments

@mistercorea
Copy link

in examples/chat/hub.go

case m := <-h.broadcast:
            for c := range h.connections {
                select {
                case c.send <- m:
                default:
                    close(c.send)
                    delete(h.connections, c)
                }
            }
        }

by default was necessary to have close and delete?

I thought closing connection would be handled by unregister call?
I am actually trying to understand the code so I can implement my own utility.

Thank you for the support in advance.

@garyburd
Copy link
Contributor

garyburd commented Nov 6, 2015

The call to close ensures that the writePump function will exit.

@mistercorea
Copy link
Author

how do i add the label? i am trying to add the label but i cant do it. this is a question btw

@garyburd
Copy link
Contributor

garyburd commented Nov 6, 2015

Are you referring to the issue label? That can only be set by an admin.

@mistercorea
Copy link
Author

oh that make sense. thanks

@mistercorea
Copy link
Author

I have another quick question, hopefully, so when connection is upgraded from main -> h.run -> wsServe ->h.register
it will do goroutine to do writePump so when message is sent it will distribute the messages.
and readPump to get original message sent from broswer?

if so when h.run() while loop is running <-h.broadcast is ran to call c.send?? what is c.send to?? i guess c.send if rom websocket library?

I am tracing back the code to see how it works and how it is implemented and having little trouble with it I am sorry to throw newbie question.

@mistercorea
Copy link
Author

nvm I think I am very confused. almost everything that is related I/O handled in WritePump and ReadPump

@garyburd
Copy link
Contributor

garyburd commented Nov 9, 2015

Should I assume nvm means that you figured it out? If not, what are the remaining questions?

@mistercorea
Copy link
Author

I have a feeling to understand this fully I have to read conn.go, client.go and server.go probably.
If i do come up with more question I will post here or PM you directly.

Thank you for your answers.

@garyburd
Copy link
Contributor

garyburd commented Nov 9, 2015

You might find this helpful: http://gary.burd.info/go-websocket-chat/

@mistercorea
Copy link
Author

Yes perfect this is the content i wanted !!!!
Thanks

@mistercorea
Copy link
Author

type my_struct strcut{
        socket_connection *tls.Conn
        ..... other user information
}

// connections.
type hub struct {
    // Registered connections.
    connections map[*connection]bool
    conn_pair map[*connection]my_struct // pairing connection with websocket with some socket connection.

    // Inbound messages from the connections.
    broadcast chan []byte

    // Register requests from the connections.
    register chan *connection

    // Unregister requests from connections.
    unregister chan *connection
}


var h = hub{
    broadcast:   make(chan []byte),
    register:    make(chan *connection),
    unregister:  make(chan *connection),
    connections: make(map[*connection]bool),
    conn_pair:   make(map[*connection]my_struct),
}

I have quick question why cant i create map with map[_connection]my_struct while map[_connection]bool is valid?

@garyburd
Copy link
Contributor

I have quick question why cant i create map with map[connection]my_struct while map[connection]bool is valid?

I don't expect either to work because connection is not comparable.

Try map[*connection]my_struct.

@mistercorea
Copy link
Author

What do you mean? @.@ I did try map[*connection]my_struct by creating variable call conn_pair.
so when i try to call or save values h.conn_pair[c] compiler is not happy lol
I am soo lost.

@mistercorea
Copy link
Author

Ultimately I am trying to create map [*connection address value] my_struct so that i can track connections

this way i can create key server.

@mistercorea
Copy link
Author

I think I am getting trouble because my_struct has some pointer method.
and i cant call those method from map.

@mistercorea
Copy link
Author

YEY I GOT IT.

map[*connection]*my_struct

this way i can call struct pointer methods too.

@gorilla gorilla locked and limited conversation to collaborators Nov 11, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants