TeaWeb/js/client.ts

203 lines
6.8 KiB
TypeScript
Raw Normal View History

/// <reference path="log.ts" />
2018-03-24 23:38:01 +01:00
/// <reference path="voice/AudioController.ts" />
2018-02-27 17:20:49 +01:00
/// <reference path="proto.ts" />
/// <reference path="ui/view.ts" />
/// <reference path="connection.ts" />
/// <reference path="settings.ts" />
/// <reference path="ui/frames/SelectedItemInfo.ts" />
2018-02-27 17:20:49 +01:00
/// <reference path="FileManager.ts" />
/// <reference path="permission/PermissionManager.ts" />
/// <reference path="permission/GroupManager.ts" />
/// <reference path="ui/ControlBar.ts" />
2018-03-07 19:06:52 +01:00
enum DisconnectReason {
2018-03-07 20:14:36 +01:00
REQUESTED,
2018-03-07 19:06:52 +01:00
CONNECT_FAILURE,
CONNECTION_CLOSED,
CONNECTION_FATAL_ERROR,
CONNECTION_PING_TIMEOUT,
CLIENT_KICKED,
CLIENT_BANNED,
SERVER_CLOSED,
UNKNOWN
}
enum ConnectionState {
UNCONNECTED,
CONNECTING,
INITIALISING,
CONNECTED,
DISCONNECTING
}
2018-02-27 17:20:49 +01:00
enum ViewReasonId {
VREASON_USER_ACTION = 0,
VREASON_MOVED = 1,
VREASON_SYSTEM = 2,
VREASON_TIMEOUT = 3,
VREASON_CHANNEL_KICK = 4,
VREASON_SERVER_KICK = 5,
VREASON_BAN = 6,
VREASON_SERVER_STOPPED = 7,
VREASON_SERVER_LEFT = 8,
VREASON_CHANNEL_UPDATED = 9,
VREASON_EDITED = 10,
VREASON_SERVER_SHUTDOWN = 11
}
class TSClient {
channelTree: ChannelTree;
serverConnection: ServerConnection;
voiceConnection: VoiceConnection;
fileManager: FileManager;
selectInfo: InfoBar;
permissions: PermissionManager;
groups: GroupManager;
controlBar: ControlBar;
private _clientId: number = 0;
private _ownEntry: LocalClientEntry;
constructor() {
this.selectInfo = new InfoBar(this, $("#select_info"));
this.channelTree = new ChannelTree(this, $("#channelTree"));
2018-03-07 19:06:52 +01:00
this.serverConnection = new ServerConnection(this);
2018-02-27 17:20:49 +01:00
this.fileManager = new FileManager(this);
this.permissions = new PermissionManager(this);
this.groups = new GroupManager(this);
this.voiceConnection = new VoiceConnection(this);
this._ownEntry = new LocalClientEntry(this);
this.controlBar = new ControlBar(this, $("#control_bar"));
this.channelTree.registerClient(this._ownEntry);
}
setup() {
this.controlBar.initialise();
}
2018-04-11 17:56:09 +02:00
startConnection(addr: string, identity: Identity, name?: string) {
2018-03-07 20:14:36 +01:00
if(this.serverConnection)
this.handleDisconnect(DisconnectReason.REQUESTED);
2018-02-27 17:20:49 +01:00
let idx = addr.lastIndexOf(':');
let port: number;
let host: string;
if(idx != -1) {
2018-03-24 23:38:01 +01:00
port = parseInt(addr.substr(idx + 1));
2018-02-27 17:20:49 +01:00
host = addr.substr(0, idx);
} else {
host = addr;
2018-04-16 20:38:35 +02:00
port = 9987;
2018-02-27 17:20:49 +01:00
}
2018-03-07 19:06:52 +01:00
console.log("Start connection to " + host + ":" + port);
this.channelTree.initialiseHead(addr, {host, port});
this.serverConnection.startConnection({host, port}, new HandshakeHandler(identity, name));
2018-02-27 17:20:49 +01:00
}
getClient() : LocalClientEntry { return this._ownEntry; }
getClientId(){ return this._clientId; } //TODO here
set clientId(id: number) {
this._clientId = id;
this._ownEntry["_clientId"] = id;
}
2018-03-07 19:06:52 +01:00
get clientId() {
return this._clientId;
}
2018-02-27 17:20:49 +01:00
getServerConnection() : ServerConnection { return this.serverConnection; }
/**
* LISTENER
*/
onConnected() {
2018-03-07 19:06:52 +01:00
console.log("Client connected!");
2018-03-07 20:14:36 +01:00
this.channelTree.registerClient(this._ownEntry);
2018-04-16 20:38:35 +02:00
settings.setServer(this.channelTree.server);
2018-03-07 19:06:52 +01:00
this.permissions.requestPermissionList();
2018-04-16 20:38:35 +02:00
this.serverConnection.sendCommand("channelsubscribeall");
2018-03-07 19:06:52 +01:00
if(this.groups.serverGroups.length == 0)
this.groups.requestGroups();
this.controlBar.updateProperties();
2018-02-27 17:20:49 +01:00
}
get connected() : boolean {
return !!this.serverConnection && this.serverConnection.connected;
}
private certAcceptUrl() {
// document.URL
let callback = document.URL;
if(document.location.search.length == 0)
callback += "?default_connect_url=true";
else
callback += "&default_connect_url=true";
//
switch (this.serverConnection._handshakeHandler.identity.type()) {
case IdentitifyType.TEAFORO:
callback += "&default_connect_type=teaforo";
break;
case IdentitifyType.TEAMSPEAK:
callback += "&default_connect_type=teamspeak";
break;
}
callback += "&default_connect_url=" + encodeURIComponent(this.serverConnection._remote_address.host + ":" + this.serverConnection._remote_address.port);
return "https://" + this.serverConnection._remote_address.host + ":" + this.serverConnection._remote_address.port + "/?forward_url=" + encodeURIComponent(callback);
}
2018-03-07 19:06:52 +01:00
handleDisconnect(type: DisconnectReason, data: any = {}) {
switch (type) {
2018-03-07 20:14:36 +01:00
case DisconnectReason.REQUESTED:
break;
2018-03-07 19:06:52 +01:00
case DisconnectReason.CONNECT_FAILURE:
console.error("Could not connect to remote host! Exception");
console.error(data);
//TODO test for status 1006
2018-03-07 19:06:52 +01:00
createErrorModal(
"Could not connect",
"Could not connect to remote host (Connection refused)<br>" +
"If you're sure that the remote host is up, than you may not allow unsigned certificates.<br>" +
"Click <a href='" + this.certAcceptUrl() + "'>here</a> to accept the remote certificate"
2018-03-07 19:06:52 +01:00
).open();
break;
case DisconnectReason.CONNECTION_CLOSED:
console.error("Lost connection to remote server!");
createErrorModal(
"Connection closed",
"The connection was closed by remote host"
).open();
break;
case DisconnectReason.CONNECTION_PING_TIMEOUT:
console.error("Connection ping timeout");
createErrorModal(
"Connection lost",
"Lost connection to remote host (Ping timeout)<br>Even possible?"
).open();
break;
case DisconnectReason.SERVER_CLOSED:
2018-03-24 23:38:01 +01:00
chat.serverChat().appendError("Server closed ({0})", data.reasonmsg);
2018-03-07 19:06:52 +01:00
createErrorModal(
"Server closed",
"The server is closed.<br>" +
"Reason: " + data.reasonmsg
).open();
break;
default:
console.error("Got uncaught disconnect!");
console.error("Type: " + type + " Data:");
console.error(data);
break;
}
2018-02-27 17:20:49 +01:00
this.selectInfo.setCurrentSelected(null);
2018-03-07 19:06:52 +01:00
this.channelTree.reset();
this.voiceConnection.dropSession();
2018-03-07 20:14:36 +01:00
if(this.serverConnection) this.serverConnection.disconnect();
2018-02-27 17:20:49 +01:00
}
}