-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathExample.html
53 lines (44 loc) · 1.78 KB
/
Example.html
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
<html>
<script src="JSMQ.js"></script>
<script>
var dealer = new JSMQ.Dealer();
dealer.connect("ws://localhost");
// we must wait for the dealer to be connected before we can send messages, any messages we are trying to send
// while the dealer is not connected will be dropped
dealer.sendReady = function() {
document.getElementById("sendButton").disabled = "";
};
var subscriber = new JSMQ.Subscriber();
subscriber.connect("ws://localhost:81");
subscriber.subscribe("chat");
subscriber.onMessage = function (message) {
// we ignore the first frame because it's topic
message.popString();
document.getElementById("chatTextArea").value =
document.getElementById("chatTextArea").value +
message.popString() + "\n";
};
dealer.onMessage = function (message) {
// the response from the server
alert(message.popString());
};
function send() {
var message = new JSMQ.Message();
message.addString(document.getElementById("messageTextBox").value);
dealer.send(message);
}
function onInputKeyPress(e) {
var unicode = e.keyCode ? e.keyCode : e.charCode;
if (unicode == 13)
send();
}
</script>
<body>
<textarea id="chatTextArea" readonly="readonly"></textarea>
<br/>
<label>Message:</label><input id="messageTextBox" value="" onkeypress="onInputKeyPress(event)"/>
<button id="sendButton" disabled="disabled" onclick="javascript:send();">
Send
</button>
</body>
</html>