在同一个websocket中多次读写

First of all, context:

  • I'm fairly new to go, I've take one 5 weeks course, but thats about it.
  • I'm all new to networking, and I've been playing around with websockets (in go) for a couple of days.

I'm working in a small team and I've been tasked with developing an IRC-server (in go) using Websockets. In order for me to be able to make sure that the server works, I've written a small testing framework (also in go).

Now to the problem at hand:
The problem is in this snippet of code.

enc := json.NewEncoder(ws)
creator := g.UserID{Name: _name, UUID: g.GenerateUID(_name + _subject)}
ac := g.DiscussionCreate{Subject: _subject, Creator: creator}
err = enc.Encode(ac) //This encoding IS read by the server.
Log.Err(err, "enc.Encode")

dec := json.NewDecoder(ws)
var disc, empty g.Discussion
err = dec.Decode(&disc)
Log.Err(err, "dec.Decode")

send := g.Message{
    Msg:  "Hello, World!",
    UDID: disc.DiscussionID.UDID,
    UUID: creator.UUID}
err = enc.Encode(send) //This encoding ISN'T read by the server.
Log.Err(err, "enc.Encode")

The server reads the first encoded message.
But fails to read the second one.

The first message is read in this function

func Create(ws *websocket.Conn) {
    Log.Connection(ws)
    var dc g.DiscussionCreate
    var di g.Discussion

    dec := json.NewDecoder(ws)
    err := dec.Decode(&dc) //Here <-----
    Log.Err(err, "dec.Decode")

    discID := g.DiscussionID{
        Subject: dc.Subject,
        UDID:    g.GenerateUID(time.Now().String())}

    ssUserID := g.SSUserID{
        Name: dc.Creator.Name,
        UUID: dc.Creator.UUID,
        WS:   ws}

    disc := g.SSDiscussion{
        DiscussionID: discID,
        Participants: []g.SSUserID{ssUserID}}

    g.LivingDiscussions = append(g.LivingDiscussions, disc)
    Log.Activity("Discussion", "Created", disc.DiscussionID.Subject)

    di = g.Discussion{
        DiscussionID: discID,
        Participants: []g.UserID{dc.Creator}}

    enc := json.NewEncoder(ws)
    err = enc.Encode(di)
    Log.Err(err, "enc.Encode")

    go g.LivingDiscussions[len(g.LivingDiscussions)-1].Participants[0].Listen()
}

And the second message is read in the go routine started in the last line of the function above.

func (ssUserID *SSUserID) Listen() {
    lastMessage := time.Now().Second()
    dec := json.NewDecoder(ssUserID.WS)
    empty := ""
    var msg Message
    for (lastMessage + ConnTimeout) > time.Now().Second() {
        if err := dec.Decode(&msg); err == nil { //Here  <-----
            Messages <- msg
            lastMessage = time.Now().Second()
        } else if err := dec.Decode(&empty); err == nil {
            lastMessage = time.Now().Second()
        }
    }
}

Why wont the server read the second message?
And how do i solve this?
Thanks!

The x/net/websocket package closes the connection on return from the handler. To fix this, do the work in the handler function instead of starting a goroutine:

g.LivingDiscussions[len(g.LivingDiscussions)-1].Participants[0].Listen()