|
1
|
+ |
package ws
|
|
2
|
+ |
|
|
3
|
+ |
import (
|
|
4
|
+ |
"context"
|
|
5
|
+ |
"fmt"
|
|
6
|
+ |
"io"
|
|
7
|
+ |
"os"
|
|
8
|
+ |
"time"
|
|
9
|
+ |
"wssrv/log"
|
|
10
|
+ |
|
|
11
|
+ |
"github.com/gorilla/websocket"
|
|
12
|
+ |
)
|
|
13
|
+ |
|
|
14
|
+ |
func readMessage(ctx context.Context, cancel context.CancelCauseFunc, con *websocket.Conn) {
|
|
15
|
+ |
var (
|
|
16
|
+ |
err error
|
|
17
|
+ |
msg []byte
|
|
18
|
+ |
)
|
|
19
|
+ |
for err != io.EOF {
|
|
20
|
+ |
select {
|
|
21
|
+ |
case <-ctx.Done():
|
|
22
|
+ |
return
|
|
23
|
+ |
default:
|
|
24
|
+ |
if _, msg, err = con.ReadMessage(); err != nil {
|
|
25
|
+ |
// chErr <- fmt.Errorf("unable to read from websocket on '%s': %v", con.RemoteAddr(), err)
|
|
26
|
+ |
cancel(fmt.Errorf("unable to read from websocket on '%s': %v", con.RemoteAddr(), err))
|
|
27
|
+ |
return
|
|
28
|
+ |
}
|
|
29
|
+ |
fmt.Printf("(%s): %s\n", con.RemoteAddr(), msg)
|
|
30
|
+ |
}
|
|
31
|
+ |
}
|
|
32
|
+ |
}
|
|
33
|
+ |
|
|
34
|
+ |
func sendFile(ctx context.Context, cancel context.CancelCauseFunc, con *websocket.Conn, fileNa string, data []byte, interv time.Duration) {
|
|
35
|
+ |
var err error
|
|
36
|
+ |
for {
|
|
37
|
+ |
select {
|
|
38
|
+ |
case <-ctx.Done():
|
|
39
|
+ |
return
|
|
40
|
+ |
default:
|
|
41
|
+ |
log.Info.Printf("sending file '%s'", fileNa)
|
|
42
|
+ |
if err = con.WriteMessage(websocket.TextMessage, data); err != nil {
|
|
43
|
+ |
// chErr <- fmt.Errorf("unable write on websocket on '%s': %v", con.RemoteAddr(), err)
|
|
44
|
+ |
cancel(fmt.Errorf("unable write on websocket on '%s': %v", con.RemoteAddr(), err))
|
|
45
|
+ |
return
|
|
46
|
+ |
}
|
|
47
|
+ |
log.Info.Printf("sleeping for '%v'", interv)
|
|
48
|
+ |
time.Sleep(interv)
|
|
49
|
+ |
}
|
|
50
|
+ |
}
|
|
51
|
+ |
|
|
52
|
+ |
}
|
|
53
|
+ |
|
|
54
|
+ |
func sendFileByInterval(con *websocket.Conn, fileNa string, interv time.Duration) error {
|
|
55
|
+ |
var (
|
|
56
|
+ |
err error
|
|
57
|
+ |
data []byte
|
|
58
|
+ |
|
|
59
|
+ |
ctx, cancel = context.WithCancelCause(context.Background())
|
|
60
|
+ |
)
|
|
61
|
+ |
|
|
62
|
+ |
if data, err = os.ReadFile(fileNa); err != nil {
|
|
63
|
+ |
return fmt.Errorf("unable to read file '%s': %v", fileNa, err)
|
|
64
|
+ |
}
|
|
65
|
+ |
|
|
66
|
+ |
go readMessage(ctx, cancel, con)
|
|
67
|
+ |
go sendFile(ctx, cancel, con, fileNa, data, interv)
|
|
68
|
+ |
|
|
69
|
+ |
<-ctx.Done()
|
|
70
|
+ |
return context.Cause(ctx)
|
|
71
|
+ |
}
|
|
72
|
+ |
|
|
73
|
+ |
// SendFileByInterval repeatedly sends a given file in the websocket according
|
|
74
|
+ |
// to an interval.
|
|
75
|
+ |
func SendFileByInterval(con *websocket.Conn, fileNa string, interv time.Duration) error {
|
|
76
|
+ |
switch {
|
|
77
|
+ |
case con == nil:
|
|
78
|
+ |
return fmt.Errorf("nil connection parameter")
|
|
79
|
+ |
case len(fileNa) == 0:
|
|
80
|
+ |
return fmt.Errorf("empty file parameter")
|
|
81
|
+ |
case interv < 1:
|
|
82
|
+ |
return fmt.Errorf("non positive interval parameter")
|
|
83
|
+ |
}
|
|
84
|
+ |
if err := checkFile(fileNa); err != nil {
|
|
85
|
+ |
return err
|
|
86
|
+ |
}
|
|
87
|
+ |
|
|
88
|
+ |
return sendFileByInterval(con, fileNa, interv)
|
|
89
|
+ |
}
|