-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.1.0-0.3.0 - Feature: TCP Socket (#24)
* Add tcpSocket option for direct connections * Dynamically import socket wrapper * Increment version * Update README
- Loading branch information
1 parent
e910827
commit 22b84ee
Showing
6 changed files
with
77 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { Socket } from 'net' | ||
import type { Buffer } from 'buffer' | ||
|
||
//**Wraps a TCP socket with the WebSocket API */ | ||
class SocketWrapper { | ||
public onopen?: () => void | ||
public onclose?: () => void | ||
public onerror?: (error: { message: string }) => void | ||
public onmessage?: (event: { data: ArrayBuffer }) => void | ||
public send: (message: Buffer) => void | ||
public close: () => void | ||
|
||
constructor(connection: string, socket: Socket) { | ||
socket.on('connect', () => { | ||
this.onopen && this.onopen() | ||
}) | ||
|
||
socket.on('close', () => { | ||
this.onclose && this.onclose() | ||
}) | ||
|
||
socket.on('error', (error) => { | ||
this.onerror && this.onerror(error) | ||
}) | ||
|
||
socket.on('data', (data) => { | ||
this.onmessage && this.onmessage({ data }) | ||
}) | ||
|
||
this.send = (message: Buffer) => { | ||
socket.write(message) | ||
} | ||
|
||
this.close = () => { | ||
socket.removeAllListeners() | ||
socket.destroy() | ||
} | ||
|
||
const url = new URL(connection) | ||
const { host } = url | ||
const [nodeIP, port] = host.split(':') | ||
|
||
socket.connect(parseInt(port), nodeIP) | ||
} | ||
} | ||
|
||
export default SocketWrapper |
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