Skip to content

Commit d551d41

Browse files
20 - Sending Client-side Messages via WebSockets
1 parent 27896ab commit d551d41

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,25 @@ const MyWebSocket = () => {
4949
}
5050
}
5151

52+
const performSend = _ => {
53+
if (socket && socket.readyState == WebSocket.OPEN) {
54+
const myData = {cell_data: "print(\"hello world\")"}
55+
socket.send(JSON.stringify(myData))
56+
} else {
57+
alert("Your websocket session has closed")
58+
}
59+
60+
}
61+
5262
return <div>
5363
<h1>WebSocket</h1>
5464
<div>
5565
<button onClick={performClose}>Close</button>
5666
<button onClick={performOpen}>Open</button>
5767
</div>
5868
<div>{isOpen && <p>Open Socket</p>} </div>
69+
70+
<button onClick={performSend}>Send</button>
5971
</div>
6072
}
6173

0 commit comments

Comments
 (0)