<commit_before>const url = require('url');
const WebSocket = require('ws');

var wss;

/* Initializes the WebSocket on the given server. */
function start(server) {
	wss = new WebSocket.Server({ server, perMessageDeflate: false });
	wss.on('connection', onConnection);
}

/* Called when a client connects to the WebSocket. */
function onConnection(ws) {
	const location = url.parse(ws.upgradeReq.url, true);

	// On message received from client
	ws.on('message', function(message) {onIncomingMessage(message, ws)});

	// When each client first connects
	ws.send('Connection opened.');
}

/* Called when the server receives a message from a client. */
function onIncomingMessage(message, ws) {
	// Broadcast the echoed message
	wss.clients.forEach(function each(client) {
		if (client.readyState === WebSocket.OPEN) {
			client.send(message);
		}
	});
}



module.exports = {start: start};<commit_msg>Keep track of connections explicitly in room-connections.<commit_after>const url = require('url');
const WebSocket = require('ws');

var wss;

// Set of sockets to clients.
var room = new Set();

/* Initializes the WebSocket on the given server. */
function start(server) {
	wss = new WebSocket.Server({ server, perMessageDeflate: false });
	wss.on('connection', onConnection);
}

/* Called when a client connects to the WebSocket. */
function onConnection(client) {
	// Set the event listeners.
	client.on('message', function(message) {onIncomingMessage(message, client)});
	client.on('close', function(message) {onClose(message, client)});

	// Keep track of the newly joined client.
	room.add(client);

	client.send('Connection opened.');
}

/* Called when the server receives a message from a client. */
function onIncomingMessage(message, sendingClient) {
	// Broadcast the echoed message
	room.forEach(function each(client) {
		if (client.readyState === WebSocket.OPEN) {
			client.send(message);
		}
	});
}

/* Called when a client closes the WebSocket connection. */
function onClose(code, reason, client) {
	room.delete(client);
}



module.exports = {start: start};
{'fraction_non_alphanumeric': 0.13542168674698796, 'fraction_numerical': 0.0, 'mean_word_length': 4.595687331536388, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1261935', 'n_tokens_mistral': 640, 'n_tokens_neox': 609, 'n_words': 191}