Compare commits
No commits in common. "36ba716ad808fa946ce3e724384cd7a50de3ea2e" and "98c89f0702374d7883eb09673d058acb9aa35654" have entirely different histories.
36ba716ad8
...
98c89f0702
|
|
@ -5,7 +5,7 @@
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>InstantChat</title>
|
<title>InstantChat</title>
|
||||||
<link rel="stylesheet" href="css/style.css">
|
<link rel="stylesheet" href="css/style.css">
|
||||||
<!-- <script src="js/jquery-3.3.1.js"></script> -->
|
<script src="js/jquery-3.3.1.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
|
|
@ -13,7 +13,6 @@
|
||||||
|
|
||||||
<script src="js/settings.js"></script>
|
<script src="js/settings.js"></script>
|
||||||
<script src="js/client.js"></script>
|
<script src="js/client.js"></script>
|
||||||
<script src="js/ui.js"></script>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -4,76 +4,55 @@ class Client {
|
||||||
constructor(wsUri) {
|
constructor(wsUri) {
|
||||||
this.wsUri = wsUri;
|
this.wsUri = wsUri;
|
||||||
|
|
||||||
// Create WebSocket and set internal callbacks
|
// Create WebSocket and set callbacks
|
||||||
console.log("Initialize Client...")
|
console.log("Initialize Client...")
|
||||||
this.webSocket = new WebSocket(wsUri);
|
this.webSocket = new WebSocket(wsUri);
|
||||||
this.webSocket.onopen = this._onSocketOpen.bind(this);
|
this.webSocket.onopen = this.onOpen.bind(this);
|
||||||
this.webSocket.onclose = this._onSocketClose.bind(this);
|
this.webSocket.onclose = this.onClose.bind(this);
|
||||||
this.webSocket.onerror = this._onSocketError.bind(this);
|
this.webSocket.onmessage = this.onMessage.bind(this);
|
||||||
this.webSocket.onmessage = this._onSocketMessage.bind(this);
|
this.webSocket.onerror = this.onError.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal WebSocket event handlers
|
// WebSocket event handlers
|
||||||
_onSocketOpen(evt) {
|
onOpen(evt) {
|
||||||
console.log("Connected to " + this.wsUri);
|
console.log("Connected to " + this.wsUri);
|
||||||
|
|
||||||
// Send init command containing chat ID and nickname
|
// Send init command containing chat ID and nickname
|
||||||
this.sendInit();
|
this.sendInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
_onSocketClose(evt) {
|
onClose(evt) {
|
||||||
console.log("Connection closed (code " + evt.code + ").");
|
console.log("Connection closed (code " + evt.code + ").");
|
||||||
}
|
}
|
||||||
|
|
||||||
_onSocketError(evt) {
|
onMessage(evt) {
|
||||||
|
console.log("Received: " + evt.data);
|
||||||
|
this.parseMessage(evt.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
onError(evt) {
|
||||||
console.error("Connection error: ", evt);
|
console.error("Connection error: ", evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onSocketMessage(evt) {
|
// Command senders
|
||||||
console.log("Received: " + evt.data);
|
|
||||||
this._parseMessage(evt.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends an arbitrary command as JSON.
|
|
||||||
*
|
|
||||||
* commandObj: The command as an object ('action' specifies type of command).
|
|
||||||
*/
|
|
||||||
sendCommand(commandObj) {
|
|
||||||
const commandJson = JSON.stringify(commandObj);
|
|
||||||
console.log("Sending command: " + commandJson);
|
|
||||||
this.webSocket.send(commandJson);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends the 'init' command which sets up the session. Also sets the chat ID and nickname.
|
|
||||||
*/
|
|
||||||
sendInit() {
|
sendInit() {
|
||||||
this.sendCommand({
|
// Define command as JSON object
|
||||||
|
let initObj = {
|
||||||
action: "init",
|
action: "init",
|
||||||
chat_id: "42",
|
chat_id: "42",
|
||||||
nickname: "binaryDiv",
|
nickname: "binaryDiv"
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Send command as JSON string
|
||||||
|
let initJson = JSON.stringify(initObj);
|
||||||
|
console.log("Sending init: " + initJson);
|
||||||
|
this.webSocket.send(initJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Message parsing
|
||||||
* Sends the 'message' command which sends a chat message to the chat.
|
parseMessage(msgString) {
|
||||||
*
|
|
||||||
* msgText: The text of the chat message.
|
|
||||||
*/
|
|
||||||
sendChatMessage(msgText) {
|
|
||||||
this.sendCommand({
|
|
||||||
action: "message",
|
|
||||||
text: msgText,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parses an incoming JSON message and dispatches specific events.
|
|
||||||
*/
|
|
||||||
_parseMessage(msgString) {
|
|
||||||
try {
|
try {
|
||||||
const msg = JSON.parse(msgString);
|
let msg = JSON.parse(msgString);
|
||||||
|
|
||||||
switch (msg.type) {
|
switch (msg.type) {
|
||||||
// Response to "init" command
|
// Response to "init" command
|
||||||
|
|
@ -99,3 +78,9 @@ class Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run script after page is loaded
|
||||||
|
$(function() {
|
||||||
|
const wsUri = AppSettings.serverWsUri;
|
||||||
|
let client = new Client(wsUri);
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
/**
|
// Global settings object
|
||||||
* Global settings object.
|
|
||||||
*/
|
|
||||||
const AppSettings = {
|
const AppSettings = {
|
||||||
// Alternative WebSocket URI for local testing:
|
|
||||||
// serverWsUri: "ws://localhost:32715",
|
// serverWsUri: "ws://localhost:32715",
|
||||||
serverWsUri: "wss://chat.glitch-in.space:443/ws/",
|
serverWsUri: "wss://chat.glitch-in.space:443/ws/",
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
"use strict";
|
|
||||||
|
|
||||||
// Global object for debugging purposes
|
|
||||||
let client;
|
|
||||||
|
|
||||||
// Execute this on start (wrapped in an anonymous function)
|
|
||||||
(function() {
|
|
||||||
const wsUri = AppSettings.serverWsUri;
|
|
||||||
client = new Client(wsUri);
|
|
||||||
})();
|
|
||||||
Loading…
Reference in New Issue