コード例 #1
0
ファイル: chatserver.go プロジェクト: eric-fouillet/gochat
func (cs *ChatServer) ReadMessage(conn net.Conn) (*gochat.ChatMessage, error) {
	buf := make([]byte, 1024)
	readBytes, err := bufio.NewReader(conn).Read(buf)
	if err != nil {
		return nil, err
	}
	var receivedMsg = new(gochat.ChatMessage)
	if err := proto.Unmarshal(buf[:readBytes], receivedMsg); err != nil {
		return nil, err
	}
	log.Printf("Read: %v from %v on %v\n", receivedMsg.GetContent(), receivedMsg.GetSender(), conn.RemoteAddr())
	return receivedMsg, nil
}
コード例 #2
0
ファイル: chatserver.go プロジェクト: eric-fouillet/gochat
// setClient: Adds a client to a chat server
// Also notifies other users that this user has connected
func (cs *ChatServer) newClient(loginMsg *gochat.ChatMessage, conn net.Conn) *ServerClient {
	from := loginMsg.GetSender()
	log.Printf("Adding new client %v to client list\n", from)
	newClient := &ServerClient{conn, from, make(chan gochat.ChatMessage)}
	cs.Clients.PushBack(newClient)
	msg := fmt.Sprintf("User %v has connected", from)
	sendTime := uint64(time.Now().Unix())
	var response = gochat.ChatMessage{
		Sender:   &from,
		SendTime: &sendTime,
		Content:  &msg,
	}
	cs.Messages <- response
	return newClient
}