Skip to content
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
39 changes: 0 additions & 39 deletions .eslintrc.json

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/client.js
/server.js
/node_modules
/dist
go.test
46 changes: 33 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,50 @@
# Install dependencies
install:
npm install
go get all

# Launch watch
# Update dependencies
update:
npm update
go get -u all

# Launch client dev server
start:
npm run start

# Launch client watcher
watch:
npx webpack --watch --mode=development
npm run watch

# Build lib
build:
npx webpack --mode=production
npm run build

# Launch demo client
demo-client:
php -S 0.0.0.0:8000 -t .
# Launch NodeJS demo server
server-node:
node ./demo/server.js 8002

# Launch demo server
demo-server:
node ./demo-server.js 8002
## Launch Golang demo server
server-go:
go run demo/server.go

# Lint and code style fix
lint:
npx eslint src/* --ext .js,.json --fix
lint: lint-js lint-go

lint-js:
npm run lint

lint-go:
gofmt -s -w .

# Test
test: build
npx mocha
test: test-js test-go

test-js:
npm run test

test-go:
go test ./go/...

# Publish package
publish: build
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ Features:
- ⚡️ Handle the binary encoding and decoding of your data, with performances in mind.
- 📢 Listen for event dispatched over websocket with simple `on`/`off` event emitter system.
- 💬 Fallback to JSON for easy debugging.
- 🌟 **NEW**: [experimental Golang port available](doc/go.md)

## Requirements

- Node >= v8.0.0
- Node >= v24.0.0

## Installation

Expand All @@ -26,11 +27,10 @@ An event is defined by its _unique_ name and the corresponding codec, responsibl

```javascript
// events.js
import Int8Codec from 'netcode/src/encoder/codec/Int8Codec';
import StringCodec from 'netcode/src/encoder/codec/StringCodec';
import { UInt8Codec, StringCodec } from 'netcode/encoder';

export default [
['id', new Int8Codec()],
['id', new UInt8Codec()],
['say', new StringCodec()],
];
```
Expand All @@ -53,8 +53,7 @@ We setup a server specifying the port and host on which the server will listen a
Here we use a BinaryEncoder to communicate in binary over websocket, with the previously configured event list.

```javascript
import Server from 'netcode/src/server/Server';
import BinaryEncoder from 'netcode/src/encoder/BinaryEncoder';
import { Server, BinaryEncoder } from 'netcode/server';
import events from './events';

// Listen on localhost:8080
Expand All @@ -68,15 +67,14 @@ server.on('client:join', client => {

Now we've got a server running at `localhost:8080` that listen for a `say` text event and send a `id` integer event to every client that connects.

_See an [full example of server setup](demo-server.js)._
_See an [full example of server setup](demo/server.js)._

### Write a Client

Now we write a client, for the browser, that connects to our running server on `ws://localhost:8080` and use a BinaryEncoder with the same event list as the server.

```javascript
import Client from 'netcode/src/client/Client';
import BinaryEncoder from 'netcode/src/encoder/BinaryEncoder';
import { Client, BinaryEncoder } from 'netcode/client';
import events from './events';

const client = new Client('ws://localhost:8080', new BinaryEncoder(events))
Expand All @@ -91,7 +89,7 @@ Now we've got client that listen for the `id` event and sent a sentence in a `sa

Connection is alive and well!

_See an [full example of client setup](demo-client.js)._
_See an [full example of client setup](demo/client.js)._

## Complete documentation

Expand Down
63 changes: 0 additions & 63 deletions demo-client.js

This file was deleted.

62 changes: 0 additions & 62 deletions demo-server.js

This file was deleted.

86 changes: 86 additions & 0 deletions demo/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Client, BinaryEncoder, UInt8Codec, UIntLongCodec, Int16Codec, BooleanCodec, StringLongCodec } from 'netcode/client';

window.addEventListener('load', () => {
const target = document.getElementById('output');
const { info } = console;

console.info = (message, ...subst) => {
info(message, ...subst);
subst.forEach(sub => message = message.replace('%s', sub));
const time = Date.now() - start;
output.innerText += `▸ ${time}ms | ${message}\n`;
}

// Register your events
const encoder = new BinaryEncoder([
['id', new UInt8Codec()],
['ping', new UIntLongCodec(6)],
['pong', new UIntLongCodec(6)],
['inverse', new BooleanCodec()],
['greeting', new StringLongCodec()],
['total', new UInt8Codec()],
['int16', new Int16Codec()],
]);


const server = 'ws://127.0.0.1:8002';
const start = Date.now();
console.info('Connecting to %s', server)

// Create the client
const client = new Client(server, encoder);
let ping;

// Listen for a "pong" event
client.on('pong', ({ detail: pong }) => {
console.info('pong: %s ms (%s - %s)', pong - ping, pong, ping);
});

// Listen for an "id" event
client.on('id', ({ detail: id }) => {
console.info('connected with id %s', id);
ping = Date.now();

console.info('sending ping: %s', ping);

// Send a "ping" event
client.send('ping', ping);
});

// Listen for a "total" event
client.on('total', ({ detail: total }) => {
console.info(`There is ${total} people connected.`);
});

// Listen for a "int16" event
client.on('int16', ({ detail: value }) => {
console.info(`int16 codec: ${value}.`);
client.send('int16', -32768);
});

// Listen for an "inverse" event
client.on('inverse', ({ detail: status }) => {
// Answer with an "inverse" event
client.send('inverse', !status);
console.info('Inverse received: %s', status);

// Send a "greeting" event
client.send('greeting', 'Hello, I\'m client 😊! Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut imperdiet molestie libero, ut sollicitudin tortor dignissim quis. Nulla iaculis nisi turpis, a malesuada nibh faucibus a. Nunc tellus lorem, varius sit amet tellus eu, dictum consectetur nulla.');
});

// Listen for a "greeting" event
client.on('greeting', ({ detail: message }) => {
console.info('Servers geets you: "%s"', message);
});

// Listen for oppening connection
client.on('open', () => {
console.info('Connection open.');
setTimeout(() => client.close(1000, 'All good :)'), 10 * 1000);
});

// Listen for connection close
client.on('close', (data) => {
console.info('Connection closed.', data);
});
});
Loading