forked from PrismarineJS/prismarine-web-client
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add WebSocket server support for direct connections! (no proxy …
…url is used then)
- Loading branch information
Showing
12 changed files
with
151 additions
and
39 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
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
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
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
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
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
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,52 @@ | ||
import { Duplex } from 'stream' | ||
|
||
class CustomDuplex extends Duplex { | ||
constructor (options, public writeAction) { | ||
super(options) | ||
} | ||
|
||
override _read () {} | ||
|
||
override _write (chunk, encoding, callback) { | ||
this.writeAction(chunk) | ||
callback() | ||
} | ||
} | ||
|
||
export const getWebsocketStream = async (host: string) => { | ||
host = host.replace('ws://', '').replace('wss://', '') | ||
const ws = new WebSocket(`ws://${host}`) | ||
const clientDuplex = new CustomDuplex(undefined, data => { | ||
ws.send(data) | ||
}) | ||
|
||
ws.addEventListener('message', async message => { | ||
let { data } = message | ||
if (data instanceof Blob) { | ||
data = await data.arrayBuffer() | ||
} | ||
clientDuplex.push(Buffer.from(data)) | ||
}) | ||
|
||
ws.addEventListener('close', () => { | ||
console.log('ws closed') | ||
clientDuplex.end() | ||
}) | ||
|
||
ws.addEventListener('error', err => { | ||
console.log('ws error', err) | ||
}) | ||
|
||
await new Promise((resolve, reject) => { | ||
ws.addEventListener('open', resolve) | ||
ws.addEventListener('error', err => { | ||
console.log('ws error', err) | ||
reject(err) | ||
}) | ||
}) | ||
|
||
return { | ||
mineflayerStream: clientDuplex, | ||
ws, | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.