Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex // timetocode committed Jan 31, 2023
0 parents commit 5b3206e
Show file tree
Hide file tree
Showing 8 changed files with 6,552 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# nengi-websocket-client-adapter
nengi client adapter for standard browser websockets
11 changes: 11 additions & 0 deletions build/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ClientNetwork, Context } from 'nengi';
declare class WebSocketClientAdapter {
socket: WebSocket | null;
network: ClientNetwork;
context: Context;
constructor(network: ClientNetwork);
flush(): void;
setupWebsocket(socket: WebSocket): void;
connect(wsUrl: string, handshake: any): Promise<unknown>;
}
export { WebSocketClientAdapter };
78 changes: 78 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebSocketClientAdapter = void 0;
const nengi_1 = require("nengi");
const nengi_dataviews_1 = require("nengi-dataviews");
class WebSocketClientAdapter {
constructor(network) {
this.socket = null;
this.network = network;
this.context = this.network.client.context;
}
flush() {
if (!this.socket) {
console.log('CANCELED, no socket');
return;
}
if (this.socket.readyState !== 1) {
console.log('socket not open');
return;
}
const buffer = this.network.createOutboundBuffer(nengi_dataviews_1.DataViewWriter);
this.socket.send(buffer);
}
setupWebsocket(socket) {
this.socket = socket;
socket.onmessage = (event) => {
if (event.data instanceof ArrayBuffer) {
const dr = new nengi_dataviews_1.DataViewReader(event.data, 0);
this.network.readSnapshot(dr);
}
};
socket.onclose = function (event) {
console.log('sock closed');
console.log(event);
// TODO
};
socket.onerror = function (event) {
console.log('socket error');
console.log(event);
// TODO
};
}
connect(wsUrl, handshake) {
return new Promise((resolve, reject) => {
const socket = new WebSocket(wsUrl);
socket.binaryType = 'arraybuffer';
socket.onopen = (event) => {
socket.send(this.network.createHandshakeBuffer(handshake, nengi_dataviews_1.DataViewWriter));
};
socket.onclose = function (event) {
reject(event);
};
socket.onerror = function (event) {
reject(event);
};
socket.onmessage = (event) => {
// initially the only thing we care to read is a response to our handshake
// we don't even setup the parser for the rest of what a nengi client can receive
const dr = new nengi_dataviews_1.DataViewReader(event.data, 0);
const type = dr.readUInt8(); // type of message
if (type === nengi_1.BinarySection.EngineMessages) {
const count = dr.readUInt8(); // quantity of engine messages
const connectionResponseByte = dr.readUInt8();
if (connectionResponseByte === nengi_1.EngineMessage.ConnectionAccepted) {
// setup listeners for normal game data
this.setupWebsocket(socket);
resolve('accepted');
}
if (connectionResponseByte === nengi_1.EngineMessage.ConnectionDenied) {
const denyReason = JSON.parse(dr.readString());
reject(denyReason);
}
}
};
});
}
}
exports.WebSocketClientAdapter = WebSocketClientAdapter;
Loading

0 comments on commit 5b3206e

Please sign in to comment.