-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The actual connect function is commented out, because there's no reason for it to do anything. But it can actually connect to a server now.
- Loading branch information
Showing
6 changed files
with
115 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
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,45 @@ | ||
/** | ||
* Connection library | ||
* | ||
* @author Guangcong Luo <guangcongluo@gmail.com> | ||
* @license MIT | ||
*/ | ||
|
||
declare var SockJS: any; | ||
|
||
class PSConnection { | ||
socket: any = null; | ||
connected = false; | ||
queue = [] as string[]; | ||
constructor() { | ||
// this.connect(); | ||
} | ||
connect() { | ||
const server = PS.server; | ||
const port = server.protocol === 'https' ? '' : ':' + server.port; | ||
const url = server.protocol + '://' + server.host + port + server.prefix; | ||
const socket = this.socket = new SockJS(url); | ||
socket.onopen = () => { | ||
console.log('\u2705 (CONNECTED)'); | ||
this.connected = true; | ||
for (const msg of this.queue) socket.send(msg); | ||
this.queue = []; | ||
}; | ||
socket.onmessage = (e: MessageEvent) => { | ||
PS.receive('' + e.data); | ||
}; | ||
socket.onclose = () => { | ||
console.log('\u2705 (DISCONNECTED)'); | ||
this.socket = null; | ||
}; | ||
} | ||
send(msg: string) { | ||
if (!this.connected) { | ||
this.queue.push(msg); | ||
return; | ||
} | ||
this.socket.send(msg); | ||
} | ||
} | ||
|
||
PS.connection = new PSConnection(); |
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