
The JSON-API can't _really_ use regular http requests because this server then has to do a lot of multi-threading nonsense. For the sake of simplicity for myself and others that try to write their own FC compliant servers: the rtc server(for now) only takes in websocket requests, and attemptes to discern servers from users connections for event handling
70 lines
1.2 KiB
JavaScript
70 lines
1.2 KiB
JavaScript
class Event {
|
|
/**
|
|
* @param raw {String}
|
|
*
|
|
*/
|
|
constructor(raw) {
|
|
this._raw = raw
|
|
let obj = JSON.parse(raw)
|
|
this.type = obj['type']
|
|
if(this.type == 'message') {
|
|
this.message = obj['message']
|
|
} else if (this.type == 'channel') {
|
|
this.channel = obj['channel']
|
|
}
|
|
}
|
|
}
|
|
|
|
class Peer {
|
|
/*
|
|
* @param type {String}
|
|
* @param channel {String}
|
|
*/
|
|
constructor(type, channel) {
|
|
this.type = type
|
|
this.channel = channel
|
|
}
|
|
}
|
|
|
|
exports.PeerMap = class {
|
|
/*
|
|
* @field users Map<Socket, Peer>
|
|
* @field server Map<Socket, Peer>
|
|
*/
|
|
constructor() {
|
|
// Map of sockets -> Peer
|
|
this.users = {}
|
|
this.server = {}
|
|
}
|
|
|
|
/**
|
|
* @params message {String|JSON} Used for emitting server messages to user peers
|
|
*/
|
|
notify(message) {
|
|
let e_type = new Event(message)
|
|
if(e_type.type) {
|
|
for(const conn of Object.keys(this.users)) {
|
|
}
|
|
} else {
|
|
console.log('[WSS] Invalid event type given from server')
|
|
}
|
|
}
|
|
|
|
// As the names imply these methods help maintain the peer map
|
|
add_server(socket) {
|
|
this.server[socket] = new Peer('server', null)
|
|
}
|
|
remove_server(socket) {
|
|
delete this.server[socket]
|
|
}
|
|
add_user(socket, channel) {
|
|
this.users[socket] = new Peer('user', channel)
|
|
}
|
|
remove_user(socket) {
|
|
delete this.user[socket]
|
|
}
|
|
|
|
}
|
|
|
|
|