Skip to content

Instantly share code, notes, and snippets.

@leopay
Last active June 17, 2019 08:40
Show Gist options
  • Save leopay/9867a223e8c5de8713c8d8bdd29a1843 to your computer and use it in GitHub Desktop.
Save leopay/9867a223e8c5de8713c8d8bdd29a1843 to your computer and use it in GitHub Desktop.
var webWs = new WebSocket("ws://192.168.147.157:8443/ws");
webWs.onopen = () => {
webWs.send(JSON.stringify({
cmd:"register",
clientid:"web",
roomid:"adminroom"
})
);
};
webWs.onmessage = evt => {
console.log("web client received:", evt.data);
var data = JSON.parse(evt.data);
if (data.msg != "") {
var msg = JSON.parse(data.msg);
switch (msg.type){
case 'req':
webWs.send(JSON.stringify({
cmd:'invite',
clientid: 'web',
roomid: 'adminroom',
msg: JSON.stringify({
type:'accept',
groupid: msg.groupid
})
}));
}
}
};
var deviceWs = new WebSocket("ws://192.168.147.157:8443/ws");
deviceWs.onmessage = function(evt) {
console.info("device received: ", evt.data)
};
deviceWs.onopen = () => {
/*
window.onbeforeunload = () => {
deviceWs.send(JSON.stringify({
cmd:'send',
msg: JSON.stringify({type: 'bye'})
})
);
};
*/
deviceWs.send(JSON.stringify({
cmd: 'register',
roomid: 'adminroom',
clientid: 'xxxxxxxxxxxx'
})
);
setTimeout(function() {
if (deviceWs.readyState == 1) {
deviceWs.send(JSON.stringify({
msg: JSON.stringify({
"type": "req",
"groupid": "123"
}),
cmd: "invite",
roomid: "adminroom",
clientid: "xxxxxxxxxxxx"
}));
}
}, 1000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment