mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
Add the new websocket package (which is just a helper for kataras/neffos) and an example for go server, client, browser client and nodejs client. Add a .fossa.yml and the generated NOTICE file for 3rd-party libs. Update go.mod, go.sum. Update the vendor folder for pongo2 to its latest master as well
Former-commit-id: 89c05079415977d65e7328a1eb8a1c602d76f78a
This commit is contained in:
93
_examples/websocket/basic/browser/index.html
Normal file
93
_examples/websocket/basic/browser/index.html
Normal file
@@ -0,0 +1,93 @@
|
||||
<!-- the message's input -->
|
||||
<input id="input" type="text" />
|
||||
|
||||
<!-- when clicked then a websocket event will be sent to the server, at this example we registered the 'chat' -->
|
||||
<button id="sendBtn" disabled>Send</button>
|
||||
|
||||
<!-- the messages will be shown here -->
|
||||
<pre id="output"></pre>
|
||||
<!-- import the iris client-side library for browser from a CDN or locally.
|
||||
However, `neffos.(min.)js` is a NPM package too so alternatively,
|
||||
you can use it as dependency on your package.json and all nodejs-npm tooling become available:
|
||||
see the "browserify" example for more-->
|
||||
<script src="https://cdn.jsdelivr.net/npm/neffos.js@0.1.8/dist/neffos.min.js"></script>
|
||||
<script>
|
||||
// `neffos` global variable is available now.
|
||||
var scheme = document.location.protocol == "https:" ? "wss" : "ws";
|
||||
var port = document.location.port ? ":" + document.location.port : "";
|
||||
var wsURL = scheme + "://" + document.location.hostname + port + "/echo";
|
||||
|
||||
var outputTxt = document.getElementById("output");
|
||||
function addMessage(msg) {
|
||||
outputTxt.innerHTML += msg + "\n";
|
||||
}
|
||||
|
||||
function handleError(reason) {
|
||||
console.log(reason);
|
||||
window.alert(reason);
|
||||
}
|
||||
|
||||
function handleNamespaceConnectedConn(nsConn) {
|
||||
let inputTxt = document.getElementById("input");
|
||||
let sendBtn = document.getElementById("sendBtn");
|
||||
|
||||
sendBtn.disabled = false;
|
||||
sendBtn.onclick = function () {
|
||||
const input = inputTxt.value;
|
||||
inputTxt.value = "";
|
||||
nsConn.emit("chat", input);
|
||||
addMessage("Me: " + input);
|
||||
};
|
||||
}
|
||||
|
||||
async function runExample() {
|
||||
// You can omit the "default" and simply define only Events, the namespace will be an empty string"",
|
||||
// however if you decide to make any changes on this example make sure the changes are reflecting inside the ../server.go file as well.
|
||||
try {
|
||||
const conn = await neffos.dial(wsURL, {
|
||||
default: { // "default" namespace.
|
||||
_OnNamespaceConnected: function (nsConn, msg) {
|
||||
addMessage("connected to namespace: " + msg.Namespace);
|
||||
handleNamespaceConnectedConn(nsConn)
|
||||
},
|
||||
_OnNamespaceDisconnect: function (nsConn, msg) {
|
||||
addMessage("disconnected from namespace: " + msg.Namespace);
|
||||
},
|
||||
chat: function (nsConn, msg) { // "chat" event.
|
||||
addMessage(msg.Body);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// You can either wait to conenct or just conn.connect("connect")
|
||||
// and put the `handleNamespaceConnectedConn` inside `_OnNamespaceConnected` callback instead.
|
||||
// const nsConn = await conn.connect("default");
|
||||
// handleNamespaceConnectedConn(nsConn);
|
||||
conn.connect("default");
|
||||
|
||||
} catch (err) {
|
||||
handleError(err);
|
||||
}
|
||||
}
|
||||
|
||||
runExample();
|
||||
|
||||
// If "await" and "async" are available, use them instead^, all modern browsers support those,
|
||||
// all of the javascript examples will be written using async/await method instead of promise then/catch callbacks.
|
||||
// A usage example of promise then/catch follows:
|
||||
// neffos.dial(wsURL, {
|
||||
// default: { // "default" namespace.
|
||||
// _OnNamespaceConnected: function (ns, msg) {
|
||||
// addMessage("connected to namespace: " + msg.Namespace);
|
||||
// },
|
||||
// _OnNamespaceDisconnect: function (ns, msg) {
|
||||
// addMessage("disconnected from namespace: " + msg.Namespace);
|
||||
// },
|
||||
// chat: function (ns, msg) { // "chat" event.
|
||||
// addMessage(msg.Body);
|
||||
// }
|
||||
// }
|
||||
// }).then(function (conn) {
|
||||
// conn.connect("default").then(handleNamespaceConnectedConn).catch(handleError);
|
||||
// }).catch(handleError);
|
||||
</script>
|
||||
Reference in New Issue
Block a user