{"action":"created","comment":{"url":"https://api.github.com/repos/dthtvwls/wsmax/pulls/comments/33185544","id":33185544,"diff_hunk":"@@ -1,30 +1,58 @@\n-// Log memory usage before doing GC. Seems to blow up the file limit sooner than without.\n-var gcLog = function () {\n-  console.log(new Date().toISOString() + JSON.stringify(process.memoryUsage()));\n-  gc();\n-};\n+var fs = require('fs');\n+var ws = require('ws');\n+var http = require('http');\n+var randomBytes = require('crypto').randomBytes;\n \n-// Tell original client how many clients are connected\n-var reporter = function () {\n-  if (this.clients[0]) this.clients[0].send(this.clients.length.toString());\n-};\n+var html = fs.readFileSync('./index.html');\n+var browsers = [];\n \n-// Garbage collect every 30 seconds\n-setInterval(gc, 30 * 1000);\n+var www = http.createServer(function (req, res) {\n+  res.writeHead(200, { 'Content-Type': 'text/html' });\n+  res.end(html);\n+}).listen(process.env.PORT || 5000);\n \n-// Create the WS server\n-new (require('ws').Server)({\n+var wss = new ws.Server({\n+  server: www\n+}).on('connection', function (socket) {\n+  socket.on('message', onmessage);\n+  socket.on('close', logSocketCount);\n+  logSocketCount();\n+});\n \n-  // Attach it to an HTTP server that serves a tiny web client\n-  server: require('http').createServer(function (req, res) {\n+// Log memory usage before doing GC. Seems to blow up the file limit sooner than without.\n+setInterval(logMemoryUsage, 30 * 1000);\n \n-    res.writeHead(200, { 'Content-Type': 'text/html' });\n-    res.end('<script>new WebSocket(\"ws://\"+location.host).onmessage=function(msg){document.body.innerText=msg.data;};</script>');\n+function logMemoryUsage () {\n+  console.log(new Date().toISOString() + JSON.stringify(process.memoryUsage()));\n+  gc();\n+}\n \n-  }).listen(process.env.PORT || 5000)\n+function getSocketCount () {\n+  return wss.clients.length.toString();\n+}\n \n-// Call the reporter on connection and close events\n-}).on('connection', function (connection) {\n-  reporter.call(this);\n-  connection.on('close', reporter.bind(this));\n-});\n+function logSocketCount () {\n+  var n = getSocketCount();\n+  browsers.forEach(function (b) {\n+    b.readyState === 1 && b.send(n);\n+  });","path":"server.js","position":54,"original_position":61,"commit_id":"1f47fde2bf1fdfdbf3b0eafed2db5036229da9bb","original_commit_id":"5a9b7ec9b74f906729339b2a25f3a477c48d3d38","user":{"login":"jessetane","id":452396,"avatar_url":"https://avatars.githubusercontent.com/u/452396?v=3","gravatar_id":"","url":"https://api.github.com/users/jessetane","html_url":"https://github.com/jessetane","followers_url":"https://api.github.com/users/jessetane/followers","following_url":"https://api.github.com/users/jessetane/following{/other_user}","gists_url":"https://api.github.com/users/jessetane/gists{/gist_id}","starred_url":"https://api.github.com/users/jessetane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jessetane/subscriptions","organizations_url":"https://api.github.com/users/jessetane/orgs","repos_url":"https://api.github.com/users/jessetane/repos","events_url":"https://api.github.com/users/jessetane/events{/privacy}","received_events_url":"https://api.github.com/users/jessetane/received_events","type":"User","site_admin":false},"body":"I guess the second link should have been to this, since we're in node-land here:\r\n\r\nhttps://github.com/websockets/ws/blob/master/lib/WebSocket.js#L212","created_at":"2015-06-24T19:25:46Z","updated_at":"2015-06-24T19:25:46Z","html_url":"https://github.com/dthtvwls/wsmax/pull/3#discussion_r33185544","pull_request_url":"https://api.github.com/repos/dthtvwls/wsmax/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/dthtvwls/wsmax/pulls/comments/33185544"},"html":{"href":"https://github.com/dthtvwls/wsmax/pull/3#discussion_r33185544"},"pull_request":{"href":"https://api.github.com/repos/dthtvwls/wsmax/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/dthtvwls/wsmax/pulls/3","id":38458094,"html_url":"https://github.com/dthtvwls/wsmax/pull/3","diff_url":"https://github.com/dthtvwls/wsmax/pull/3.diff","patch_url":"https://github.com/dthtvwls/wsmax/pull/3.patch","issue_url":"https://api.github.com/repos/dthtvwls/wsmax/issues/3","number":3,"state":"open","locked":false,"title":"Thor","user":{"login":"jessetane","id":452396,"avatar_url":"https://avatars.githubusercontent.com/u/452396?v=3","gravatar_id":"","url":"https://api.github.com/users/jessetane","html_url":"https://github.com/jessetane","followers_url":"https://api.github.com/users/jessetane/followers","following_url":"https://api.github.com/users/jessetane/following{/other_user}","gists_url":"https://api.github.com/users/jessetane/gists{/gist_id}","starred_url":"https://api.github.com/users/jessetane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jessetane/subscriptions","organizations_url":"https://api.github.com/users/jessetane/orgs","repos_url":"https://api.github.com/users/jessetane/repos","events_url":"https://api.github.com/users/jessetane/events{/privacy}","received_events_url":"https://api.github.com/users/jessetane/received_events","type":"User","site_admin":false},"body":"The \"realism\" I was looking for comes in when you start moving data around. I swapped out the phantom test and changed the server a bit so that it treats web browser and thor connections differently - browsers get a ui, test connections bounce messages back and forth.","created_at":"2015-06-24T16:00:27Z","updated_at":"2015-06-24T19:25:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8f954befd21b1699ef78c2d8f3042e44c11ed539","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dthtvwls/wsmax/pulls/3/commits","review_comments_url":"https://api.github.com/repos/dthtvwls/wsmax/pulls/3/comments","review_comment_url":"https://api.github.com/repos/dthtvwls/wsmax/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dthtvwls/wsmax/issues/3/comments","statuses_url":"https://api.github.com/repos/dthtvwls/wsmax/statuses/1f47fde2bf1fdfdbf3b0eafed2db5036229da9bb","head":{"label":"jessetane:thor","ref":"thor","sha":"1f47fde2bf1fdfdbf3b0eafed2db5036229da9bb","user":{"login":"jessetane","id":452396,"avatar_url":"https://avatars.githubusercontent.com/u/452396?v=3","gravatar_id":"","url":"https://api.github.com/users/jessetane","html_url":"https://github.com/jessetane","followers_url":"https://api.github.com/users/jessetane/followers","following_url":"https://api.github.com/users/jessetane/following{/other_user}","gists_url":"https://api.github.com/users/jessetane/gists{/gist_id}","starred_url":"https://api.github.com/users/jessetane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jessetane/subscriptions","organizations_url":"https://api.github.com/users/jessetane/orgs","repos_url":"https://api.github.com/users/jessetane/repos","events_url":"https://api.github.com/users/jessetane/events{/privacy}","received_events_url":"https://api.github.com/users/jessetane/received_events","type":"User","site_admin":false},"repo":{"id":37962831,"name":"wsmax","full_name":"jessetane/wsmax","owner":{"login":"jessetane","id":452396,"avatar_url":"https://avatars.githubusercontent.com/u/452396?v=3","gravatar_id":"","url":"https://api.github.com/users/jessetane","html_url":"https://github.com/jessetane","followers_url":"https://api.github.com/users/jessetane/followers","following_url":"https://api.github.com/users/jessetane/following{/other_user}","gists_url":"https://api.github.com/users/jessetane/gists{/gist_id}","starred_url":"https://api.github.com/users/jessetane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jessetane/subscriptions","organizations_url":"https://api.github.com/users/jessetane/orgs","repos_url":"https://api.github.com/users/jessetane/repos","events_url":"https://api.github.com/users/jessetane/events{/privacy}","received_events_url":"https://api.github.com/users/jessetane/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jessetane/wsmax","description":"","fork":true,"url":"https://api.github.com/repos/jessetane/wsmax","forks_url":"https://api.github.com/repos/jessetane/wsmax/forks","keys_url":"https://api.github.com/repos/jessetane/wsmax/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jessetane/wsmax/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jessetane/wsmax/teams","hooks_url":"https://api.github.com/repos/jessetane/wsmax/hooks","issue_events_url":"https://api.github.com/repos/jessetane/wsmax/issues/events{/number}","events_url":"https://api.github.com/repos/jessetane/wsmax/events","assignees_url":"https://api.github.com/repos/jessetane/wsmax/assignees{/user}","branches_url":"https://api.github.com/repos/jessetane/wsmax/branches{/branch}","tags_url":"https://api.github.com/repos/jessetane/wsmax/tags","blobs_url":"https://api.github.com/repos/jessetane/wsmax/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jessetane/wsmax/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jessetane/wsmax/git/refs{/sha}","trees_url":"https://api.github.com/repos/jessetane/wsmax/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jessetane/wsmax/statuses/{sha}","languages_url":"https://api.github.com/repos/jessetane/wsmax/languages","stargazers_url":"https://api.github.com/repos/jessetane/wsmax/stargazers","contributors_url":"https://api.github.com/repos/jessetane/wsmax/contributors","subscribers_url":"https://api.github.com/repos/jessetane/wsmax/subscribers","subscription_url":"https://api.github.com/repos/jessetane/wsmax/subscription","commits_url":"https://api.github.com/repos/jessetane/wsmax/commits{/sha}","git_commits_url":"https://api.github.com/repos/jessetane/wsmax/git/commits{/sha}","comments_url":"https://api.github.com/repos/jessetane/wsmax/comments{/number}","issue_comment_url":"https://api.github.com/repos/jessetane/wsmax/issues/comments{/number}","contents_url":"https://api.github.com/repos/jessetane/wsmax/contents/{+path}","compare_url":"https://api.github.com/repos/jessetane/wsmax/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jessetane/wsmax/merges","archive_url":"https://api.github.com/repos/jessetane/wsmax/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jessetane/wsmax/downloads","issues_url":"https://api.github.com/repos/jessetane/wsmax/issues{/number}","pulls_url":"https://api.github.com/repos/jessetane/wsmax/pulls{/number}","milestones_url":"https://api.github.com/repos/jessetane/wsmax/milestones{/number}","notifications_url":"https://api.github.com/repos/jessetane/wsmax/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jessetane/wsmax/labels{/name}","releases_url":"https://api.github.com/repos/jessetane/wsmax/releases{/id}","created_at":"2015-06-24T04:45:38Z","updated_at":"2015-06-24T04:45:38Z","pushed_at":"2015-06-24T18:18:12Z","git_url":"git://github.com/jessetane/wsmax.git","ssh_url":"git@github.com:jessetane/wsmax.git","clone_url":"https://github.com/jessetane/wsmax.git","svn_url":"https://github.com/jessetane/wsmax","homepage":null,"size":80,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dthtvwls:master","ref":"master","sha":"cd6bc1e6a5993c6219efc24c4595f2fc95306c42","user":{"login":"dthtvwls","id":258992,"avatar_url":"https://avatars.githubusercontent.com/u/258992?v=3","gravatar_id":"","url":"https://api.github.com/users/dthtvwls","html_url":"https://github.com/dthtvwls","followers_url":"https://api.github.com/users/dthtvwls/followers","following_url":"https://api.github.com/users/dthtvwls/following{/other_user}","gists_url":"https://api.github.com/users/dthtvwls/gists{/gist_id}","starred_url":"https://api.github.com/users/dthtvwls/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dthtvwls/subscriptions","organizations_url":"https://api.github.com/users/dthtvwls/orgs","repos_url":"https://api.github.com/users/dthtvwls/repos","events_url":"https://api.github.com/users/dthtvwls/events{/privacy}","received_events_url":"https://api.github.com/users/dthtvwls/received_events","type":"User","site_admin":false},"repo":{"id":35708994,"name":"wsmax","full_name":"dthtvwls/wsmax","owner":{"login":"dthtvwls","id":258992,"avatar_url":"https://avatars.githubusercontent.com/u/258992?v=3","gravatar_id":"","url":"https://api.github.com/users/dthtvwls","html_url":"https://github.com/dthtvwls","followers_url":"https://api.github.com/users/dthtvwls/followers","following_url":"https://api.github.com/users/dthtvwls/following{/other_user}","gists_url":"https://api.github.com/users/dthtvwls/gists{/gist_id}","starred_url":"https://api.github.com/users/dthtvwls/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dthtvwls/subscriptions","organizations_url":"https://api.github.com/users/dthtvwls/orgs","repos_url":"https://api.github.com/users/dthtvwls/repos","events_url":"https://api.github.com/users/dthtvwls/events{/privacy}","received_events_url":"https://api.github.com/users/dthtvwls/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dthtvwls/wsmax","description":"","fork":false,"url":"https://api.github.com/repos/dthtvwls/wsmax","forks_url":"https://api.github.com/repos/dthtvwls/wsmax/forks","keys_url":"https://api.github.com/repos/dthtvwls/wsmax/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dthtvwls/wsmax/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dthtvwls/wsmax/teams","hooks_url":"https://api.github.com/repos/dthtvwls/wsmax/hooks","issue_events_url":"https://api.github.com/repos/dthtvwls/wsmax/issues/events{/number}","events_url":"https://api.github.com/repos/dthtvwls/wsmax/events","assignees_url":"https://api.github.com/repos/dthtvwls/wsmax/assignees{/user}","branches_url":"https://api.github.com/repos/dthtvwls/wsmax/branches{/branch}","tags_url":"https://api.github.com/repos/dthtvwls/wsmax/tags","blobs_url":"https://api.github.com/repos/dthtvwls/wsmax/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dthtvwls/wsmax/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dthtvwls/wsmax/git/refs{/sha}","trees_url":"https://api.github.com/repos/dthtvwls/wsmax/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dthtvwls/wsmax/statuses/{sha}","languages_url":"https://api.github.com/repos/dthtvwls/wsmax/languages","stargazers_url":"https://api.github.com/repos/dthtvwls/wsmax/stargazers","contributors_url":"https://api.github.com/repos/dthtvwls/wsmax/contributors","subscribers_url":"https://api.github.com/repos/dthtvwls/wsmax/subscribers","subscription_url":"https://api.github.com/repos/dthtvwls/wsmax/subscription","commits_url":"https://api.github.com/repos/dthtvwls/wsmax/commits{/sha}","git_commits_url":"https://api.github.com/repos/dthtvwls/wsmax/git/commits{/sha}","comments_url":"https://api.github.com/repos/dthtvwls/wsmax/comments{/number}","issue_comment_url":"https://api.github.com/repos/dthtvwls/wsmax/issues/comments{/number}","contents_url":"https://api.github.com/repos/dthtvwls/wsmax/contents/{+path}","compare_url":"https://api.github.com/repos/dthtvwls/wsmax/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dthtvwls/wsmax/merges","archive_url":"https://api.github.com/repos/dthtvwls/wsmax/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dthtvwls/wsmax/downloads","issues_url":"https://api.github.com/repos/dthtvwls/wsmax/issues{/number}","pulls_url":"https://api.github.com/repos/dthtvwls/wsmax/pulls{/number}","milestones_url":"https://api.github.com/repos/dthtvwls/wsmax/milestones{/number}","notifications_url":"https://api.github.com/repos/dthtvwls/wsmax/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dthtvwls/wsmax/labels{/name}","releases_url":"https://api.github.com/repos/dthtvwls/wsmax/releases{/id}","created_at":"2015-05-16T03:56:47Z","updated_at":"2015-05-23T17:21:45Z","pushed_at":"2015-06-24T18:18:12Z","git_url":"git://github.com/dthtvwls/wsmax.git","ssh_url":"git@github.com:dthtvwls/wsmax.git","clone_url":"https://github.com/dthtvwls/wsmax.git","svn_url":"https://github.com/dthtvwls/wsmax","homepage":null,"size":116,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":3,"forks":1,"open_issues":3,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dthtvwls/wsmax/pulls/3"},"html":{"href":"https://github.com/dthtvwls/wsmax/pull/3"},"issue":{"href":"https://api.github.com/repos/dthtvwls/wsmax/issues/3"},"comments":{"href":"https://api.github.com/repos/dthtvwls/wsmax/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/dthtvwls/wsmax/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/dthtvwls/wsmax/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dthtvwls/wsmax/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/dthtvwls/wsmax/statuses/1f47fde2bf1fdfdbf3b0eafed2db5036229da9bb"}}}}