Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async init() {
// launching a "mock socket server" so that we can mock a peer
this.serverProcess = fork(__dirname + "/index.js");
await delay(2000);
// client socket so we can send mocking instructions to our mock server
this.clientSocket = socketCluster.create({
port: 4009,
hostname: "127.0.0.1",
});
}
? peerConfig.connectTimeout
? peerConfig.connectTimeout
: DEFAULT_CONNECT_TIMEOUT
: DEFAULT_CONNECT_TIMEOUT,
ackTimeout: peerConfig
? peerConfig.connectTimeout
? peerConfig.connectTimeout
: DEFAULT_CONNECT_TIMEOUT
: DEFAULT_ACK_TIMEOUT,
multiplex: false,
autoConnect: false,
autoReconnect: false,
pingTimeoutDisabled: true,
};
const outboundSocket = socketClusterClient.create(clientOptions);
// Attaching handlers for various events that could be used future for logging or any other application
outboundSocket.emit(
REMOTE_EVENT_RPC_REQUEST,
{
type: '/RPCRequest',
procedure: requestPacket.procedure,
},
(err: Error | undefined, responseData: unknown) => {
if (err) {
reject(err);
return;
}
if (responseData) {
const responsePacket = responseData as P2PResponsePacket;
resolve({
], (require) => {
// Socketcluster and rpc layer.
var SC = require("socketcluster-client/index");
var SCRPC = require("avs-rpc").scRpc;
// Connect
console.log(this)
this.scConn = SC.connect();
this.rpc = new SCRPC(this.scConn);
// Connect the basic events.
this.scConn.on('error', (err) => {
console.log("Error:", err);
});
this.scConn.on('connect', () => {
// Logging
this.scConn.on("client_log", (msg) => {
console.log("REMOTE>", msg);
});
// RPC
this.scConn.on("rpc_init_data", (methods) => {
//console.log("Methods:",methods);
this.rpcr = this.rpc.remote.call(this.rpc, methods);
export const connect = (store, next, action, callSocket) => {
// initial setup
if(!memoizedSocket && !pendingConnection && action.type === SOCKET_REQUEST) {
pendingConnection = true
const socket = socketCluster.connect({
hostname: process.env.SOCKET_HOSTNAME || location.hostname,
path: process.env.SOCKET_PATH || '/ws',
autoReconnect: true,
autoReconnectOptions: process.env.AUTO_RECONNECT_OPTIONS,
authTokenName: process.env.AUTH_TOKEN_NAME,
})
attachListeners(store, next, action, socket, callSocket)
socket.on('connect', data => {
// yay! lets memoize the socket
memoizedSocket = socket
pendingConnection = false
const authToken = socket.getAuthToken()
const channels = authToken && authToken.channels || undefined
export function loadNotes() {
const query = `
subscription {
getAllNotes {
id,
title,
laneId,
userId,
index
}
}`;
const serializedParams = prepareGraphQLParams({query});
const sub = 'getAllNotes';
const socket = socketCluster.connect(socketOptions);
socket.subscribe(serializedParams, {waitForAuth: true});
return dispatch => {
// client-side changefeed handler
socket.on(sub, data => {
const meta = {synced: true};
if (!data.old_val) {
dispatch(addNote(data.new_val, meta));
} else if (!data.new_val) { // eslint-disable-line no-negated-condition
dispatch(deleteNote(data.old_val.id, meta));
} else {
dispatch(updateNote(data.new_val, meta));
}
});
socket.on('unsubscribe', channelName => {
if (channelName === sub) {
dispatch({type: CLEAR_NOTES});
private create(peer: P2P.IPeer): SCClientSocket {
const connection = create({
port: peer.port,
hostname: peer.ip,
perMessageDeflate: true,
});
const socket = (connection as any).transport.socket;
socket.on("ping", () => this.terminate(peer));
socket.on("pong", () => this.terminate(peer));
socket.on("message", data => {
if (data === "#1") {
// this is to establish some rate limit on #1 messages
// a simple rate limit of 1 per second doesnt seem to be enough, so decided to give some margin
// and allow up to 10 per second which should be more than enough
const timeNow: number = new Date().getTime();
socket._last10Pings = socket._last10Pings || [];
P2PPeerInfo,
P2PRequestPacket,
P2PResponsePacket,
ProtocolMessagePacket,
} from '../p2p_types';
import {
getNetgroup,
sanitizeNodeInfoToLegacyFormat,
validatePeerInfo,
validatePeersInfoList,
validateProtocolMessage,
validateRPCRequest,
} from '../utils';
export const socketErrorStatusCodes = {
...(socketClusterClient.SCClientSocket as any).errorStatuses,
1000: 'Intentionally disconnected',
};
// Can be used to convert a rate which is based on the rateCalculationInterval into a per-second rate.
const RATE_NORMALIZATION_FACTOR = 1000;
interface Productivity {
readonly requestCounter: number;
readonly responseCounter: number;
readonly responseRate: number;
readonly lastResponded: number;
}
export type SCClientSocket = socketClusterClient.SCClientSocket;
export type SCServerSocketUpdated = {
it('should connect with valid options', function (done) {
socket = scClient.connect(validOptions);
socket.on('connecting', function (data) {
console.log('CONNECTING...', data);
});
socket.on('connectAbort', function (data) {
done('should not reject handshake with valid params', data);
});
socket.on('connect', function (data) {
done();
});
socket.on('error', function (err) {
done(err);
});
before(function() {
socket = scClient.connect({ hostname: 'localhost', port: 8000 });
socket.connect();
socket.on('error', function(error) {
console.error('Socket1 error', error);
});
socket2 = scClient.connect({ hostname: 'localhost', port: 8000 });
socket2.connect();
socket.on('error', function(error) {
console.error('Socket2 error', error);
});
});
before(function() {
socket = scClient.connect({ hostname: 'localhost', port: 8000 });
socket.connect();
socket.on('error', function(error) {
console.error('Socket1 error', error);
});
socket2 = scClient.connect({ hostname: 'localhost', port: 8000 });
socket2.connect();
socket.on('error', function(error) {
console.error('Socket2 error', error);
});
});