Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring of terminator strings; http endpoint to list clients per channel #34

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "none",
"singleQuote": true,
"tabWidth": 2,
"useTabs": false
}
92 changes: 57 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
NoobHub
=======
# NoobHub

[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

OpenSource multiplayer and network messaging for CoronaSDK, Moai, Gideros & LÖVE
OpenSource multiplayer and network messaging for CoronaSDK, Moai, Gideros, LÖVE & Defold

Battle-tested and production ready. Handling thousands of CCU (concurrent users), serving hundreds of thousands multiplayer games daily, routing hundreds of messages per second, with months of uptime.

* Connections are routed through socket server with minimum latency, ideal for action games.
* Simple interface. Publish/subscribe paradigm in action.
* Server written on blazing fast Nodejs.
* Zero dependency. Works out of the box, no NPM ecosystem required.
* Socket connections, works great through any NAT (local area network), messages delivery is reliable and fast.
* Low CPU and memory footprint
- Connections are routed through socket server with minimum latency, ideal for action games.
- Simple interface. Publish/subscribe paradigm in action.
- Server written on blazing fast Nodejs.
- Zero dependency. Works out of the box, no NPM ecosystem required. (for websocket bridge relies on `ws` module, read below)
- Socket connections, works great through any NAT (local area network), messages delivery is reliable and fast.
- Low CPU and memory footprint

Repo includes server code (so you can use your own server) and CoronaSDK/Moai/Gideros/LÖVE client. More clients to come.
You can test on my server, credentials are hardcoded in demo project!

Lua code may serve as an example of how LuaSocket library works.


How to use it
------------
## How to use it

START SERVER

```bash
$ nodejs node.js
```

To make use of the websocket bridge, uncomment wsPort in the config.
If so, make sure to do `npm install` as well.
These are useful to serve browser clients. Irrelevant otherwise.

INITIALIZE

```lua
hub = noobhub.new({ server = "127.0.0.1"; port = 1337; });
```

```js
const hub = noobhub.new({ server: '127.0.0.1', port: 2337 });
```

SUBSCRIBE TO A CHANNEL AND RECEIVE CALLBACKS WHEN NEW JSON MESSAGES ARRIVE

```lua
hub:subscribe({
channel = "hello-world";
Expand All @@ -46,7 +54,18 @@ SUBSCRIBE TO A CHANNEL AND RECEIVE CALLBACKS WHEN NEW JSON MESSAGES ARRIVE
end;
});
```

```js
hub.subscribe({
channel: 'hello-world',
callback: (data) => {
console.log('callback', data);
}
});
```

SAY SOMETHING TO EVERYBODY ON THE CHANNEL

```lua
hub:publish({
message = {
Expand All @@ -56,18 +75,24 @@ SAY SOMETHING TO EVERYBODY ON THE CHANNEL
});
```

```js
hub.publish({
action: 'ping',
timestamp: Date.now()
});
```

## Clients

Clients
------
* CoronaSDK
* Gideros
* Moai
* LÖVE
* Node.js
* PHP (debug console only)
- CoronaSDK
- Gideros
- Moai
- LÖVE
- Node.js
- PHP (debug console only)
- JS / Browser

Tests
-----
## Tests

Simple acceptance test uses Nodejs client to test the server itself:

Expand All @@ -79,25 +104,22 @@ Simple acceptance test uses Nodejs client to test the server itself:
tests ok
```

Getting ready for production use
------------
## Getting ready for production use

If you expect more than 1000 concurrent connections, you should increase limits on your server (max open file descriptors,
max TCP/IP connections) and optionally fine-tune your server's TCP/IP stack.
To make sure server process stays alive you migh want to use tools such as forever.js or supervisord.
To make sure server process stays alive you might want to use tools such as forever.js or supervisord.

Authors
-------
## Authors

* Igor Korsakov
* Sergii Tsegelnyk
- Igor Korsakov
- Sergii Tsegelnyk

## Licence

Licence
-------
[WTFPL](http://www.wtfpl.net/txt/copying/)

Official discussion thread
---------------------------
## Official discussion thread

* [old] http://developer.coronalabs.com/code/noobhub
* [new] http://forums.coronalabs.com/topic/32775-noobhub-free-opensource-multiplayer-and-network-messaging-for-coronasdk
- [old] http://developer.coronalabs.com/code/noobhub
- [new] http://forums.coronalabs.com/topic/32775-noobhub-free-opensource-multiplayer-and-network-messaging-for-coronasdk
58 changes: 58 additions & 0 deletions client/javascript-browser/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function noop() {}

const noobhub = {
new: function (cfg) {
const ws = new WebSocket(`ws://${cfg.server}:${cfg.port}`);

let channel;
let callback;
let errorCallback;
let subscribedCallback;
let isReady = false;

function publish(data) {
isReady && ws.send(`__JSON__START__${JSON.stringify(data)}__JSON__END__`);
}

function subscribe(o) {
channel = o.channel;
callback = o.callback;
errorCallback = o.errorCallback || noop;
subscribedCallback = o.subscribedCallback || noop;
isReady &&
ws.send(JSON.stringify(`__SUBSCRIBE__${channel}__ENDSUBSCRIBE__`));
}

ws.addEventListener('open', () => {
isReady = true;
ws.send(JSON.stringify(`__SUBSCRIBE__${channel}__ENDSUBSCRIBE__`));
subscribedCallback(ws);
});

ws.addEventListener('message', (rawData) => {
let data = rawData.data;
try {
let start, end;
if (
(start = data.indexOf('__JSON__START__')) !== -1 &&
(end = data.indexOf('__JSON__END__')) !== -1
) {
const json = data.substr(start + 15, end - (start + 15));
data = JSON.parse(json);
}
} catch (_) {}
callback(data);
});

ws.addEventListener('error', (err) => {
errorCallback(err);
});

ws.addEventListener('close', () => {
isReady = false;
errorCallback('closed');
});

return { publish, subscribe };
}
};
37 changes: 37 additions & 0 deletions client/javascript-browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<html>
<head>
<meta charset="utf-8" />
<title>test noobhub websocket client</title>
</head>
<body>
<h1>test noobhub websocket client</h1>
<script src="client.js"></script>

<script>
const hub = noobhub.new({ server: 'localhost', port: 2337 });

const myName = 'js_browser_user_' + Math.floor(1000 * Math.random());
const ch = 'ch1';
const dt = 2000;

hub.subscribe({
channel: ch,
callback: (data) => {
console.log('callback', data);
},
subscribedCallback: () => {
console.log('subscribedCallback');

let i = 0;
setInterval(() => {
hub.publish({ from: myName, data: `${i++} ${Math.random()}` });
}, dt);
},
errorCallback: (err) => {
console.log('error callback', err);
process.exit(1);
}
});
</script>
</body>
</html>
Loading