Made a thin websocket like wrapper around the client API

This commit is contained in:
David Fowler 2016-10-01 14:27:50 -07:00
parent 540784e525
commit d646e3666f
2 changed files with 99 additions and 49 deletions

View File

@ -4,7 +4,9 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<title></title> <title></title>
<script> <script>
document.addEventListener('DOMContentLoaded', () => { function socket(url) {
var sock = this;
sock.url = url;
function xhr(method, url) { function xhr(method, url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -31,39 +33,63 @@
}); });
} }
function send(connectionId, data) { function poll(xhr) {
var xhr = new XMLHttpRequest(); var url = `${sock.url}/poll?id=${sock.connectionId}`;
var url = `/chat/send?id=${connectionId}`; xhr.open('POST', url, true);
xhr.open("POST", url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
}
}
xhr.send(data);
}
function poll(connectionId, xhr) {
var url = `/chat/poll?id=${connectionId}`;
xhr.open("POST", url, true);
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && xhr.status == 200) { if (xhr.readyState == 4 && xhr.status == 200) {
var child = document.createElement('li'); sock.onmessage(xhr.responseText);
child.innerText = xhr.response; poll(xhr);
document.getElementById('messages').appendChild(child);
poll(connectionId, xhr);
} }
} }
xhr.send(null); xhr.send(null);
} }
xhr('GET', '/chat/getid').then(connectionId => { this.onopen = function () { };
document.getElementById('sendmessage').addEventListener('click', () => { this.onmessage = function () { };
let data = document.getElementById('data').value; this.onerror = function (evt) { };
send(connectionId, data); this.send = function (data) {
}); if (!sock.connectionId) {
throw "Not connected";
}
var xhr = new XMLHttpRequest();
var url = `${sock.url}/send?id=${sock.connectionId}`;
xhr.open('POST', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
// Do something useful here
}
}
xhr.send(data);
};
xhr('GET', `${sock.url}/getid`).then(connectionId => {
sock.connectionId = connectionId;
sock.onopen();
poll(connectionId, new XMLHttpRequest()); poll(connectionId, new XMLHttpRequest());
}); });
}
document.addEventListener('DOMContentLoaded', () => {
var sock = new socket('/chat');
sock.onopen = function () {
console.log('Opened!');
};
sock.onmessage = function (evt) {
var child = document.createElement('li');
child.innerText = evt.data;
document.getElementById('messages').appendChild(child);
};
document.getElementById('sendmessage').addEventListener('click', () => {
let data = document.getElementById('data').value;
sock.send(data);
});
}); });
</script> </script>
</head> </head>

View File

@ -4,17 +4,9 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<title></title> <title></title>
<script> <script>
document.addEventListener('DOMContentLoaded', () => { function socket(url) {
function send(connectionId, data) { var sock = this;
var xhr = new XMLHttpRequest(); sock.url = url;
var url = `/chat/send?id=${connectionId}`;
xhr.open("POST", url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
}
}
xhr.send(data);
}
function xhr(method, url) { function xhr(method, url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -41,27 +33,61 @@
}); });
} }
xhr('GET', '/chat/getid').then(connectionId => { this.onopen = function () { };
let source = new EventSource(`/chat/sse?id=${connectionId}`); this.onmessage = function () { };
this.onerror = function (evt) { };
this.send = function (data) {
if (!sock.connectionId) {
throw "Not connected";
}
var xhr = new XMLHttpRequest();
var url = `${sock.url}/send?id=${sock.connectionId}`;
xhr.open('POST', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
}
}
xhr.send(data);
};
// Start the connection
xhr('GET', `${sock.url}/getid`).then(connectionId => {
sock.connectionId = connectionId;
let source = new EventSource(`${sock.url}/sse?id=${connectionId}`);
source.onopen = function () { source.onopen = function () {
console.log('Opened!'); sock.onopen();
}; };
source.onerror = function (err) { source.onerror = function (err) {
console.log('Error: ' + err.type); sock.onerror(err);
}; };
source.onmessage = function (data) { source.onmessage = function (data) {
var child = document.createElement('li'); sock.onmessage(data);
child.innerText = data.data;
document.getElementById('messages').appendChild(child);
}; };
});
};
document.getElementById('sendmessage').addEventListener('click', () => {
let data = document.getElementById('data').value; document.addEventListener('DOMContentLoaded', () => {
send(connectionId, data); var sock = new socket('/chat');
});
sock.onopen = function () {
console.log('Opened!');
};
sock.onmessage = function (evt) {
var child = document.createElement('li');
child.innerText = evt.data;
document.getElementById('messages').appendChild(child);
};
document.getElementById('sendmessage').addEventListener('click', () => {
let data = document.getElementById('data').value;
sock.send(data);
}); });
}); });
@ -72,8 +98,6 @@
<input type="text" id="data" /> <input type="text" id="data" />
<input type="button" id="sendmessage" value="Send" /> <input type="button" id="sendmessage" value="Send" />
<ul id="messages"> <ul id="messages"></ul>
</ul>
</body> </body>
</html> </html>