Merge pull request #1060 from thelounge/yamanickill/modularise-socket

Create socket module
This commit is contained in:
Jérémie Astori 2017-04-22 00:36:06 -04:00 committed by GitHub
commit bc5b03d2fc
2 changed files with 55 additions and 49 deletions

View File

@ -3,7 +3,6 @@
// vendor libraries // vendor libraries
require("jquery-ui/ui/widgets/sortable"); require("jquery-ui/ui/widgets/sortable");
const $ = require("jquery"); const $ = require("jquery");
const io = require("socket.io-client");
const Mousetrap = require("mousetrap"); const Mousetrap = require("mousetrap");
const URI = require("urijs"); const URI = require("urijs");
@ -15,14 +14,9 @@ const helpers_parse = require("./libs/handlebars/parse");
const helpers_roundBadgeNumber = require("./libs/handlebars/roundBadgeNumber"); const helpers_roundBadgeNumber = require("./libs/handlebars/roundBadgeNumber");
const slideoutMenu = require("./libs/slideout"); const slideoutMenu = require("./libs/slideout");
const templates = require("../views"); const templates = require("../views");
const socket = require("./socket");
$(function() { $(function() {
var path = window.location.pathname + "socket.io/";
var socket = io({
path: path,
autoConnect: false,
reconnection: false
});
var commands = [ var commands = [
"/away", "/away",
"/back", "/back",
@ -84,48 +78,6 @@ $(function() {
// available. See http://stackoverflow.com/q/14555347/1935861. // available. See http://stackoverflow.com/q/14555347/1935861.
} }
} }
[
"connect_error",
"connect_failed",
"disconnect",
"error",
].forEach(function(e) {
socket.on(e, function(data) {
$("#loading-page-message").text("Connection failed: " + data);
$("#connection-error").addClass("shown").one("click", function() {
window.onbeforeunload = null;
window.location.reload();
});
// Disables sending a message by pressing Enter. `off` is necessary to
// cancel `inputhistory`, which overrides hitting Enter. `on` is then
// necessary to avoid creating new lines when hitting Enter without Shift.
// This is fairly hacky but this solution is not permanent.
$("#input").off("keydown").on("keydown", function(event) {
if (event.which === 13 && !event.shiftKey) {
event.preventDefault();
}
});
// Hides the "Send Message" button
$("#submit").remove();
console.error(data);
});
});
socket.on("connecting", function() {
$("#loading-page-message").text("Connecting…");
});
socket.on("connect", function() {
$("#loading-page-message").text("Finalizing connection…");
});
socket.on("authorized", function() {
$("#loading-page-message").text("Authorized, loading messages…");
});
socket.on("auth", function(data) { socket.on("auth", function(data) {
var login = $("#sign-in"); var login = $("#sign-in");
var token; var token;

54
client/js/socket.js Normal file
View File

@ -0,0 +1,54 @@
"use strict";
const $ = require("jquery");
const io = require("socket.io-client");
const path = window.location.pathname + "socket.io/";
const socket = io({
path: path,
autoConnect: false,
reconnection: false
});
[
"connect_error",
"connect_failed",
"disconnect",
"error",
].forEach(function(e) {
socket.on(e, function(data) {
$("#loading-page-message").text("Connection failed: " + data);
$("#connection-error").addClass("shown").one("click", function() {
window.onbeforeunload = null;
window.location.reload();
});
// Disables sending a message by pressing Enter. `off` is necessary to
// cancel `inputhistory`, which overrides hitting Enter. `on` is then
// necessary to avoid creating new lines when hitting Enter without Shift.
// This is fairly hacky but this solution is not permanent.
$("#input").off("keydown").on("keydown", function(event) {
if (event.which === 13 && !event.shiftKey) {
event.preventDefault();
}
});
// Hides the "Send Message" button
$("#submit").remove();
console.error(data);
});
});
socket.on("connecting", function() {
$("#loading-page-message").text("Connecting…");
});
socket.on("connect", function() {
$("#loading-page-message").text("Finalizing connection…");
});
socket.on("authorized", function() {
$("#loading-page-message").text("Authorized, loading messages…");
});
module.exports = socket;