Skip to content

Commit

Permalink
_examples/basic: improvements and a how-to-run document for newcomers
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jun 12, 2019
1 parent 3b704c8 commit d82d4ec
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
28 changes: 28 additions & 0 deletions _examples/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Basic Example

## Requirements

- [NPM](https://nodejs.org)
- [Go Programming Language](https://golang.org/dl)

## How to run

Open a terminal window instance and execute:

```sh
$ cd ./browser
# build the browser-side client: ./browser/bundle.js which ./browser/index.html imports.
$ npm install && npm run-script build
$ cd ../ # go back to ./basic
$ go run main.go server # start the neffos websocket server.
```

Open some web browser windows and navigate to <http://localhost:8080>,
each window will ask for a username and a room to join, each window(client connection) and server get notified for namespace connected/disconnected, room joined/left and chat events.

To start the go client side just open a new terminal window and execute:
```sh
$ go run main.go client
```

It will ask you for username and a room to join as well, it acts exactly the same as the `./browser/app.js` browser-side application.
39 changes: 33 additions & 6 deletions _examples/basic/browser/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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";
}
Expand All @@ -22,25 +23,38 @@ class UserMessage {
}


function handleNamespaceConnectedConn(nsConn) {
async function handleNamespaceConnectedConn(nsConn) {
const roomToJoin = prompt("Please specify a room to join, i.e room1: ");
// const room = await nsConn.joinRoom(roomToJoin) with "async function handleNamespaceConnectedConn"...;
// or:
nsConn.joinRoom(roomToJoin); // and nsConn.room("roomName").leave();

let inputTxt = document.getElementById("input");
let sendBtn = document.getElementById("sendBtn");

sendBtn.disabled = false;
sendBtn.onclick = function () {
const input = inputTxt.value;
inputTxt.value = "";
const userMsg = new UserMessage(nsConn.conn.ID, input);
nsConn.emit("chat", neffos.marshal(userMsg));
addMessage("Me: " + input);

switch (input) {
case "leave":
nsConn.room(roomToJoin).leave();
// or room.leave();
break;
default:
const userMsg = new UserMessage(nsConn.conn.ID, input);
nsConn.emit("chat", neffos.marshal(userMsg));
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 {
var username = prompt("Please specify a username: ");
const username = prompt("Please specify a username: ");

const conn = await neffos.dial(wsURL, {
default: { // "default" namespace.
Expand All @@ -51,12 +65,25 @@ async function runExample() {
_OnNamespaceDisconnect: function (nsConn, msg) {
addMessage("disconnected from namespace: " + msg.Namespace);
},
_OnRoomJoined: function (nsConn, msg) {
addMessage("joined to room: " + msg.Room);
},
_OnRoomLeft: function (nsConn, msg) {
addMessage("left from room: " + msg.Room);
},
notify: function (nsConn, msg) {
addMessage(msg.Body);
},
chat: function (nsConn, msg) { // "chat" event.
const userMsg = msg.unmarshal()
addMessage(userMsg.from + ": " + userMsg.text);
}
}
}, { headers: { 'X-Username': username } });
}, {
headers: {
'X-Username': username
}
});

conn.connect("default");

Expand Down
Loading

0 comments on commit d82d4ec

Please sign in to comment.