-
-
Save kabalage/f7e1fcbd2e455c5a6924 to your computer and use it in GitHub Desktop.
socket.io client in electron
This file contains hidden or 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
| var app = require("app"); // Module to control application life. | |
| var BrowserWindow = require("browser-window"); // Module to create native browser window. | |
| var io = require("socket.io-client"); | |
| require("crash-reporter").start(); | |
| var mainWindow = null; | |
| app.on("window-all-closed", function() { | |
| // if (process.platform != "darwin") | |
| // app.quit(); | |
| }); | |
| var socket; | |
| app.on("ready", function() { | |
| console.log("app ready"); | |
| initSocket(); | |
| }); | |
| function initSocket() { | |
| console.log("initSocket"); | |
| socket = io("http://localhost:3000", { | |
| reconnectionDelay: 0, | |
| reconnectionDelayMax: 0, | |
| randomizationFactor: 0, | |
| timeout: 1000, | |
| reconnection: true | |
| }); | |
| socket.on("connect_error", function() { | |
| console.log("connect_error", arguments); | |
| }); | |
| socket.on("connect_timeout", function() { | |
| console.log("connect_timeout", arguments); | |
| }); | |
| socket.on("reconnect", function() { | |
| console.log("reconnect", arguments); | |
| }); | |
| socket.on("reconnect_attempt", function() { | |
| console.log("reconnect_attempt", arguments); | |
| }); | |
| socket.on("reconnect_error", function() { | |
| console.log("reconnect_error", arguments); | |
| }); | |
| socket.on("reconnecting", function() { | |
| console.log("reconnecting", arguments); | |
| }); | |
| socket.on("reconnect_failed", function() { | |
| console.log("reconnect_failed", arguments); | |
| }); | |
| socket.on("connect", function() { | |
| socket.emit("electronReady"); | |
| console.log("electronReady"); | |
| socket.on("open", function() { | |
| openWindow(); | |
| }); | |
| socket.on("quit", function() { | |
| app.quit(); | |
| }); | |
| }); | |
| } | |
| function openWindow() { | |
| if (!mainWindow) { | |
| mainWindow = new BrowserWindow({width: 800, height: 600}); | |
| mainWindow.loadUrl("file://" + __dirname + "/../../gui/index.html"); | |
| mainWindow.openDevTools(); | |
| mainWindow.on("closed", function() { | |
| mainWindow = null; | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment