-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.html
60 lines (51 loc) · 1.9 KB
/
html.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
54
55
56
57
58
59
60
<html>
<head>
<script type="application/javascript">
//Websocket test
var channel = window.location.hash.slice(1);
var websocket;
function load(){
websocket = new WebSocket("ws://vps.bernardbekker.nl:8080");
websocket.onopen = function(){
console.log("con opened");
websocket.send(JSON.stringify({type:"cmd", cmd:"channel", data: channel}));
websocket.send(JSON.stringify({type: "cmd", cmd:"gmsg"}));
}
websocket.onmessage = function(msg){
receive(msg.data);
}
document.getElementById("channel").textContent = channel;
}
function receive(json){
msg = JSON.parse(json);
if(msg.type == "msg"){
document.getElementById("received").textContent = msg.msg;
}
if(msg.type == "response"){
if(msg.cmd == "users"){
document.getElementById("clients").textContent = msg.data;
}
}
}
function changeChannel(chan){
channel = chan;
websocket.send(JSON.stringify({type:"cmd", cmd:"channel", data: channel}));
document.getElementById("channel").textContent = channel;
}
function send(){
var msg = document.getElementById("input").value;
var json = {type: "msg" , msg: msg};
websocket.send(JSON.stringify(json));
}
</script>
</head>
<body onload="load()">
<pre>
Websocket test
channel: <span id="channel"></span> <button onclick="changeChannel(prompt('channel'))">change</button>
Received: <span id="received">nothing :(</span>
Send: <input type="text" id="input" placeholder="message" ></input>
<button onclick="send()">Send</button>
Clients on channel: <span id="clients"></span>
</pre>
</body>