Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 27aeb9a

Browse files
committed
Implement client user agent getter
Add a getter for the user agent of a client. Remove the ostype subpackage, this library won't be responsible for parsing the user agent string due to excessive complexity. It is up to the user to parse the UA to get detailed information about the client.
1 parent 6a549e1 commit 27aeb9a

File tree

4 files changed

+8
-184
lines changed

4 files changed

+8
-184
lines changed

client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ type Client struct {
1818
conn *websocket.Conn
1919

2020
connectionTime time.Time
21+
userAgent string
2122
Session *Session
2223
}
2324

25+
// UserAgent returns the user agent string associated with this client
26+
func (clt *Client) UserAgent() string {
27+
return clt.userAgent
28+
}
29+
2430
// write sends the given data to the other side of the socket,
2531
// it also protects the connection from concurrent writes
2632
func (clt *Client) write(data []byte) error {

examples/chatroom/server/sessions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var sessions = make(map[string]*webwire.Client)
1111

1212
func onClientConnected(newClient *webwire.Client) {
1313
connectedClients[newClient] = true
14-
log.Printf("New client connected: %s", newClient.RemoteAddr())
14+
log.Printf("New client connected: %s | %s", newClient.RemoteAddr(), newClient.UserAgent())
1515
}
1616

1717
func onClientDisconnected(client *webwire.Client) {

ostype/ostype.go

Lines changed: 0 additions & 183 deletions
This file was deleted.

server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ func (srv *Server) ServeHTTP(
354354
&sync.Mutex{},
355355
conn,
356356
time.Now(),
357+
req.Header.Get("User-Agent"),
357358
nil,
358359
}
359360

0 commit comments

Comments
 (0)