Netcode provides you with 2 pre-packaged versions of the library for Node and for the browser, so you can use it out of the box. You can also import the ES6 source code directly and manage the packaging yourself with a tool like Webpack.
Let's see an example of both these setups:
On the server-side (node):
const {
Server,
BinaryEncoder,
Int16Codec,
BooleanCodec,
StringCodec,
// ...
} = require('netcode/server');`
And in the browser as well:
<body>
<!-- Defines a netcode variable in global scope -->
<script src="node_modules/netcode/client.js"></script>
<script>
const {
Client,
BinaryEncoder,
Int16Codec,
BooleanCodec,
StringCodec,
// ...
} = netcode;
</script>
</body>
Configure Webpack to include the source code of netcode so that Babel will compile this ES6 code as well.
module.exports = {
//...
module: {
rules: [{
// ...
include: '/node_modules/netcode/src/',
}]
}
};
See a full Webpack/Babel config example.
Now you can import source file from netcode directly in your sources and it will be compiled as well:
// src/server.js
import Server from 'netcode/src/server/Server';
import BinaryEncoder from 'netcode/src/encoder/BinaryEncoder';
import { BooleanCodec } from 'netcode/src/encoder/codec';
new Server(
8080,
'localhost',
new BinaryEncoder([
['foo', new BooleanEncoder()]
])
);
// src/client.js
import Client from 'netcode/src/client/Client';
import BinaryEncoder from 'netcode/src/encoder/BinaryEncoder';
import { BooleanCodec } from 'netcode/src/encoder/codec';
new Client(
'ws://localhost:8080',
new BinaryEncoder([
['foo', new BooleanCodec()]
])
);