UNPKG

network-collection

Version:

Baseline collection of graphs and network data structures using **socket.io**.

68 lines (60 loc) 2.07 kB
'use strict'; const client = require('socket.io-client'); const config = require('./config.json'); const Collection = require('../'); const port = config.port; const connections = config.connections; const Network = Collection.Network; const Graph = Collection.Graph; const Node = Collection.Node; let networks = []; let sockets = createConnections(connections); // Force new websocket connections to the cluster. function createConnections(n) { let sockets = []; for (var id = 0; id < n; id++) { sockets[id] = client('http://0.0.0.0:' + port, { transports: ['websocket'], 'force new connection': true }); sockets[id].on('connect', handleConnect.bind(null, sockets[id], id)); } return sockets; } // Populate graphs and nodes with information. function createGraphs() { let name; let graphs = {}; let nodes = {}; for (let i = 0; i < config.graphs; i++) { for (let j = 0; j < (Math.floor(Math.random() * config.items.length)); j++) { // Create a new Node. let node = new Node({ price: Math.floor(Math.random() * config.maxValue) + 1, // Give the item a random price. name: config.items[Math.floor(Math.random() * config.items.length)] // Give the item a random name. }); nodes[node.id] = node; } name = config.names.length > 1 ? config.names.splice(Math.floor(Math.random() * config.names.length), 1) : config.names[0]; // Create a new Graph. let graph = new Graph( 'stores', // Name the label 'stores' { name: name }, // Give the graph some specific data to hold { nodes: nodes } // Populate the graph with some predefined nodes. ); graphs[graph.id] = graph; } return graphs; } function handleConnect(socket, id) { // Load the network with these models. let graphs = createGraphs(); networks[id] = new Network({ graphs: graphs, }); // Connect the socket to the Cluster // The Cluster can then query this socket for information. networks[id].connect(socket, () => { console.log('Network connected!'); }); }