🎉 Initial SSE MVP

This commit is contained in:
Liyas Thomas
2019-11-24 08:48:50 +05:30
parent 1dbea4d39a
commit 873b97b052

View File

@@ -1,5 +1,9 @@
<template> <template>
<div class="page"> <div class="page">
<section id="options">
<input id="tab-one" type="radio" name="options" checked="checked" />
<label for="tab-one">WebSocket</label>
<div class="tab">
<pw-section class="blue" label="Request" ref="request"> <pw-section class="blue" label="Request" ref="request">
<ul> <ul>
<li> <li>
@@ -33,7 +37,6 @@
</div> </div>
</ul> </ul>
</pw-section> </pw-section>
<pw-section <pw-section
class="purple" class="purple"
label="Communication" label="Communication"
@@ -84,12 +87,50 @@
</li> </li>
</div> </div>
</ul> </ul>
<input type="text" name="" value="">
<button type="button" name="button" @click="start()"></button>
<div id="result"></div>
</pw-section> </pw-section>
</div> </div>
<input id="tab-two" type="radio" name="options" />
<label for="tab-two">SSE</label>
<div class="tab">
<pw-section class="blue" label="Request" ref="request">
<ul>
<li>
<label for="server">Server</label>
<input
id="server"
type="url"
:class="{ error: !serverValid }"
v-model="server"
@keyup.enter="serverValid ? toggleSSEConnection() : null"
/>
</li>
<div>
<li>
<label for="start" class="hide-on-small-screen">&nbsp;</label>
<button
:disabled="!serverValid"
id="start"
name="start"
@click="toggleSSEConnection"
>
{{ toggleSSEConnectionVerb }}
<span>
<i class="material-icons" v-if="!connectionSSEState">sync</i>
<i class="material-icons" v-if="connectionSSEState"
>sync_disabled</i
>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
<input type="text" name="" value="">
<button type="button" name="button" @click="start()"></button>
<div id="result"></div>
</div>
</section>
</div>
</template> </template>
<style lang="scss"> <style lang="scss">
div.log { div.log {
@@ -131,6 +172,13 @@ export default {
communication: { communication: {
log: null, log: null,
input: "" input: ""
},
connectionSSEState: false,
server: "wss://echo.websocket.org",
sse: null,
events: {
log: null,
input: ""
} }
}; };
}, },
@@ -149,6 +197,21 @@ export default {
"i" "i"
); );
return pattern.test(this.url); return pattern.test(this.url);
},
toggleSSEConnectionVerb() {
return !this.connectionSSEState ? "Start" : "Stop";
},
serverValid() {
const pattern = new RegExp(
"^(wss?:\\/\\/)?" +
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" +
"((\\d{1,3}\\.){3}\\d{1,3}))" +
"(\\:\\d+)?(\\/[-a-z\\d%_.~+@]*)*" +
"(\\?[;&a-z\\d%_.~+=-]*)?" +
"(\\#[-a-z\\d_]*)?$",
"i"
);
return pattern.test(this.server);
} }
}, },
methods: { methods: {
@@ -258,6 +321,12 @@ export default {
return sourceEmojis[source]; return sourceEmojis[source];
return ""; return "";
}, },
toggleSSEConnection() {
// If it is connecting:
if (!this.connectionSSEState) return this.start();
// Otherwise, it's disconnecting.
else return this.stop();
},
start() { start() {
if(typeof(EventSource) !== "undefined") { if(typeof(EventSource) !== "undefined") {
var source = new EventSource("http://wgrothaus.ucc.ie/~frank/cs3513/server_event_source.php"); var source = new EventSource("http://wgrothaus.ucc.ie/~frank/cs3513/server_event_source.php");
@@ -267,6 +336,9 @@ export default {
} else { } else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events..."; document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
} }
},
stop() {
if (this.sse != null) this.sse.close();
} }
}, },
updated: function() { updated: function() {