Skip to content

Commit

Permalink
added socket.io forced websockets feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tumerorkun committed Jan 5, 2018
1 parent f61733a commit 70cf186
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ When the server is running, you can open in your browser:

* [http://localhost:3001/](http://localhost:3001/) - WebSocket version
* [http://localhost:3002/](http://localhost:3002/) - Socket.IO version
* [http://localhost:3002/forced](http://localhost:3002/forced) - Socket.IO version (forced websockets)

In Firefox or Chrome, you can open developer tools with Shift+Ctrl+I,
open the Network tab and reload the page with Ctrl+R to see the network traffic
Expand Down
53 changes: 53 additions & 0 deletions si-forced.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!doctype html>
<!--
Socket.IO example - frontend - from:
https://github.com/rsp/node-websocket-vs-socket.io
Copyright (c) 2015, 2016 Rafał Pocztarski
Released under MIT License (Expat) - see:
https://github.com/rsp/node-websocket-vs-socket.io/blob/master/LICENSE.md
-->
<html lang=en>
<head>
<meta charset=utf-8>
<meta name=viewport content="width=device-width, initial-scale=1">
<title>socket.io example - node-websocket-vs-socket.io</title>
<style>
h1 { font-family: helvetica, sans-serif; margin: 0; }
h1+p { margin: 0; }
</style>
</head>
<body>
<h1>socket.io example</h1>
<p>
From node-websocket-vs-socket.io by Rafał Pocztarski
<br><a href="https://github.com/rsp/node-websocket-vs-socket.io">https://github.com/rsp/node-websocket-vs-socket.io</a>
<br>Comparing WebSocket vs. Socket.IO on Node.js with Express.js
</p>
<p>
Open developer tools with Shift+Ctrl+I,
select Network and reload the page.
<br>This is the socket.io example.
See also the <a href="http://localhost:3001/">websocket example</a>.
</p>
<ol id=l></ol>
<script src="/socket.io/socket.io.js"></script>
<script>

var l = document.getElementById('l');
var log = function (m) {
var i = document.createElement('li');
i.innerText = new Date().toISOString()+' '+m;
l.appendChild(i);
}
log('opening socket.io connection');
var s = io({
transports: ['websocket']
});
s.on('connect_error', function (m) { log("error"); });
s.on('connect', function (m) { log("socket.io connection open"); });
s.on('message', function (m) { log(m); });

</script>
</body>
</html>

4 changes: 4 additions & 0 deletions si.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ app.get('/', (req, res) => {
console.error('express connection');
res.sendFile(path.join(__dirname, 'si.html'));
});
app.get('/forced', (req, res) => {
console.error('express connection');
res.sendFile(path.join(__dirname, 'si-forced.html'));
});
io.on('connection', s => {
console.error('socket.io connection');
for (var t = 0; t < 3; t++)
Expand Down
4 changes: 4 additions & 0 deletions ws-vs-si.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ si.app.get('/', (req, res) => {
si.log('express connection - sending html');
res.sendFile(path.join(__dirname, 'si.html'));
});
si.app.get('/forced', (req, res) => {
si.log('express connection - sending html');
res.sendFile(path.join(__dirname, 'si-forced.html'));
});
si.io.on('connection', s => {
si.log('incoming socket.io connection');
for (var t = 0; t < 3; t++)
Expand Down

0 comments on commit 70cf186

Please sign in to comment.