Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hopefully this is good for you. #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

node_modules/
*.pem
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
# Socket.IO Fiddle

```
$ sh generate.sh # create self-signed keys for server-ssl.js and client-ssl.js
$ npm install
$ npm start # run the server
$ npm start # run the server or optionally npm run server
$ npm run client # run the nodejs client
$ npm run serverssl # run the server-ssl
$ npm run clientssl # run the nodejs client-ssl
```

And point your browser to `http://localhost:3000`. Optionally, specify
a port by supplying the `PORT` env variable.
And point your browser to either `http://localhost:3000` or `https://localhost:3000` depending on which server is running.
Optionally, specify a port by supplying the `PORT` env variable.

You will have to accept the self-signed warning. Older versions of node are known to have issues. This is a way to test your version of node.

**Known versions to work with SSL/TLS: v8.1.3 -- expect newest versions of v6 and v4 to also work (untested)!**
20 changes: 20 additions & 0 deletions client-ssl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

// for the following to work, you'll have to answer "localhost" at the Common Name question when running generate.sh
// > Common Name (e.g. server FQDN or YOUR name) []:localhost
const fs = require('fs');
const socket = require('socket.io-client')('https://localhost:3000', {
rejectUnauthorized: true, // default value
ca: fs.readFileSync('./cert.pem')
});

// USE WITH CAUTION! The following disables the validation of the server's identity
// see https://nodejs.org/docs/latest/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
// const socket = require('socket.io-client')('https://localhost:3000', {
// rejectUnauthorized: false
// });

socket.on('connect', onConnect);

function onConnect(){
console.log('connect ' + socket.id);
}
3 changes: 3 additions & 0 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo "You can optionally answer the question, or 'enter' for defaults: (recommend setting common name to localhost)"
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 3650 -out cert.pem
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
},
"scripts": {
"start": "node server.js",
"serverssl": "node server-ssl.js",
"clientssl": "node client-ssl.js",
"server": "node server.js",
"client": "node client.js"
}
}
21 changes: 21 additions & 0 deletions server-ssl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

const express = require('express');
const app = express();
const fs = require('fs');
const server = require('https').createServer({
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem'),
}, app);
const io = require('socket.io')(server);
const port = process.env.PORT || 3000;

app.use(express.static(__dirname + '/public'));

io.on('connect', onConnect);
server.listen(port, () => console.log('server listening on port ' + port));

function onConnect(socket){
console.log('connect ' + socket.id);

socket.on('disconnect', () => console.log('disconnect ' + socket.id));
}