// Recv starts listening to the ReadWriter and passes *Message to a channel
Recv starts listening to the ReadWriter and passes *Message to a channel
// Recv starts listening to the ReadWriter and passes * Message to a channel
func (c *Connection) Recv(messageOut chan<- *Message) {
	go func() {
		for {
			isCommand, body, err := c.read()
			if err != nil {
				messageOut <- &Message{Err: err, MessageType: ErrorMessage}
				return
			}
			if !isCommand {
				frames := [][]byte{body}
				messageOut <- &Message{Body: frames, MessageType: UserMessage}
			} else {
				command, err := c.parseCommand(body)
				if err != nil {
					messageOut <- &Message{Err: err, MessageType: ErrorMessage}
					return
				}
				switch command.Name {
				case "PING":
					if err := c.SendCommand("PONG", nil); err != nil {
						messageOut <- &Message{Err: err, MessageType: ErrorMessage}
						return
					}
				default:
					frames := [][]byte{command.Body}
					messageOut <- &Message{Name: command.Name, Body: frames, MessageType: ErrorMessage}
				}
			}
		}
	}()
}
func ( c * Connection ) Recv ( messageOut chan <- * Message ) { go func ( ) { for { isCommand , body , err := c . read ( ) if err != nil { messageOut <- & Message { Err : err , MessageType : ErrorMessage } return } if ! isCommand { frames := [ ] [ ] byte { body } messageOut <- & Message { Body : frames , MessageType : UserMessage } } else { command , err := c . parseCommand ( body ) if err != nil { messageOut <- & Message { Err : err , MessageType : ErrorMessage } return } switch command . Name { case " " : if err := c . SendCommand ( " " , nil ) ; err != nil { messageOut <- & Message { Err : err , MessageType : ErrorMessage } return } default : frames := [ ] [ ] byte { command . Body } messageOut <- & Message { Name : command . Name , Body : frames , MessageType : ErrorMessage } } } } } ( ) }
Recv starts listening to the ReadWriter and passes *Message to a channel
Recv starts listening to the ReadWriter and passes * Message to a channel
// Actually read out the body and send it over the channel now // Data frame // Check what type of command we got // Certain commands we deal with directly, the rest we send over to the application // When we get a ping, we want to send back a pong, we don't really care about the contents right now