-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex // timetocode
committed
Jan 31, 2023
0 parents
commit 5b3206e
Showing
8 changed files
with
6,552 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.