Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
75 lines (65 loc) · 2.39 KB
/
Copy pathserver_test.go
File metadata and controls
75 lines (65 loc) · 2.39 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package wrapper
import (
"fmt"
"log"
"net/http"
"os"
)
// This example shows how to use create a ws-server-wrapper that echoes a string
// sent to the "echo" event handler.
func ExampleServer_echo() {
// Create ws-wrapper-server and "echo" event handler. Note that the event
// handler function may optionally have a context as its first argument,
// it must always return 2 values, and the second value must implement
// error.
wsServer := NewServer()
wsServer.On("echo", func(s string) (string, error) {
// If a ws-server client sends a "echo" request, it will receive the
// echoed response.
return s, nil
})
// Create HTTP handler function that upgrades the HTTP connection to a
// WebSocket using the github.com/coder/websocket package.
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 1. Use your favorite WebSocket library to upgrade the HTTP request to
// a WebSocket connection...
// 2. Use an adapter (or write your own) to write and parse ws-wrapper
// messages.
// 3. Call `wsServer.Accept` to attach the underlying WebSocket
// connection to `wsServer`. It will start listening for inbound
// messages.
// See full example in the coder adapter.
})
// Start the HTTP server
log.Fatal(http.ListenAndServe("localhost:8080", h))
}
// Demonstrates how to add various event handlers.
func ExampleServerChannel_On() {
wsServer := NewServer()
// Simple event handler that does not return a response
// Note: Since no channel name is provided, this handler is attached to the
// "main" channel with no name.
wsServer.On("print", func(s string) (struct{}, error) {
fmt.Println(s)
return struct{}{}, nil
})
// Simple request handler that echoes whatever was received
wsServer.On("echo", func(s string) (string, error) {
return s, nil
})
// Request handler that reads a file from disk. CAUTION: Be sure to sanitize
// user input.
// Note: json.Marshall will return a base64 string to the client because
// `[]byte` is returned by the handler.
wsServer.On("readFile", func(filename string) ([]byte, error) {
return os.ReadFile(filename)
})
}
// Demonstrates adding event handlers to a particular channel.
func ExampleServerChannel_On_channel() {
wsServer := NewServer()
// Adds a "readFile" event handler to the "io" channel.
wsServer.Of("io").On("readFile", func(filename string) ([]byte, error) {
return os.ReadFile(filename)
})
}