-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.go
43 lines (37 loc) · 838 Bytes
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package chat
import (
"net"
"net/http"
"github.com/julienschmidt/httprouter"
)
type connection interface {
read() error
write(message *message) error
close()
}
// A User represents a user in the chat. Their connection is used to
// communicate
type User struct {
name string
conn connection
}
func createTCPUser(conn net.Conn, h *hub) *User {
u := newTCPUser(conn, h)
u.write(newMessage(u.currentRoomName, u.username, chatHelp, text))
return &User{
name: u.name(),
conn: u,
}
}
func createWSUser(h *hub, w http.ResponseWriter, r *http.Request, _ httprouter.Params) *User {
u, err := newWsUser(w, r, h)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
u.write(newMessage(u.currentRoomName, u.username, chatHelp, text))
return &User{
name: u.username,
conn: u,
}
}