-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
WebSocket.react.js
138 lines (120 loc) · 3.8 KB
/
WebSocket.react.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {Component} from 'react';
import PropTypes from 'prop-types';
/**
* A simple interface to
*/
export default class DashWebSocket extends Component {
_init_client() {
// Create a new client.
let {url} = this.props;
const {protocols} = this.props;
url = url? url : "ws://" + location.host + location.pathname + "ws";
this.client = new WebSocket(url, protocols);
// Listen for events.
this.client.onopen = (e) => {
// TODO: Add more properties here?
this.props.setProps({
state: {
// Mandatory props.
readyState: WebSocket.OPEN,
isTrusted: e.isTrusted,
timeStamp: e.timeStamp,
// Extra props.
origin: e.origin,
}
})
}
this.client.onmessage = (e) => {
// TODO: Add more properties here?
this.props.setProps({
message: {
data: e.data,
isTrusted: e.isTrusted,
origin: e.origin,
timeStamp: e.timeStamp
}
})
}
this.client.onerror = (e) => {
// TODO: Implement error handling.
this.props.setProps({error: JSON.stringify(e)})
}
this.client.onclose = (e) => {
// TODO: Add more properties here?
this.props.setProps({
state: {
// Mandatory props.
readyState: WebSocket.CLOSED,
isTrusted: e.isTrusted,
timeStamp: e.timeStamp,
// Extra props.
code: e.code,
reason: e.reason,
wasClean: e.wasClean,
}
})
}
}
componentDidMount() {
this._init_client()
}
componentDidUpdate(prevProps) {
const {send} = this.props;
// Send messages.
if (send && send !== prevProps.send) {
if (this.props.state.readyState === WebSocket.OPEN) {
this.client.send(send)
}
}
// TODO: Maybe add support for changing the url?
}
componentWillUnmount() {
// Clean up (close the connection).
this.client.onopen = null;
this.client.onclose = null;
this.client.onerror = null;
this.client.onmessage = null;
this.client.close();
}
render() {
return (null);
}
}
DashWebSocket.defaultProps = {
state: {readyState: WebSocket.CONNECTING}
}
DashWebSocket.propTypes = {
/**
* This websocket state (in the readyState prop) and associated information.
*/
state: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
/**
* When messages are received, this property is updated with the message content.
*/
message: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
/**
* This property is set with the content of the onerror event.
*/
error: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
/**
* When this property is set, a message is sent with its content.
*/
send: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
/**
* The websocket endpoint (e.g. wss://echo.websocket.org).
*/
url: PropTypes.string,
/**
* Supported websocket protocols (optional).
*/
protocols: PropTypes.arrayOf(PropTypes.string),
/**
* The ID used to identify this component in Dash callbacks.
*/
id: PropTypes.string,
/**
* Dash-assigned callback that should be called to report property changes
* to Dash, to make them available for callbacks.
*/
setProps: PropTypes.func
}