-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_factory.go
52 lines (44 loc) · 1.22 KB
/
client_factory.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
44
45
46
47
48
49
50
51
52
package wspubsub
import (
"net/http"
)
// WebsocketConnectionUpgrader upgrades HTTP connection to the WebSocket connection.
type WebsocketConnectionUpgrader interface {
Upgrade(w http.ResponseWriter, r *http.Request) (WebsocketConnection, error)
}
// WebsocketConnection represents a WebSocket connection.
type WebsocketConnection interface {
Read() (Message, error)
Write(message Message) error
Close() error
}
// UUIDGenerator generates UUID v4.
type UUIDGenerator interface {
GenerateV4() UUID
}
// ClientFactory is responsible for creating a client.
type ClientFactory struct {
options ClientOptions
uuidGenerator UUIDGenerator
upgrader WebsocketConnectionUpgrader
logger Logger
}
// Create returns a new client.
func (f *ClientFactory) Create() WebsocketClient {
return NewClient(f.options, f.uuidGenerator.GenerateV4(), f.upgrader, f.logger)
}
// NewClientFactory initializes a new ClientFactory.
func NewClientFactory(
options ClientOptions,
uuidGenerator UUIDGenerator,
upgrader WebsocketConnectionUpgrader,
logger Logger,
) *ClientFactory {
factory := &ClientFactory{
options: options,
uuidGenerator: uuidGenerator,
upgrader: upgrader,
logger: logger,
}
return factory
}