From 31b4c450e0a0a7142795b8ef57416af24fe06c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20G=C3=BCerere?= Date: Thu, 15 Oct 2015 17:12:11 -0430 Subject: [PATCH] Introduced a new example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This introduces a new example project that build on top of socket.io’s own tutorial. Fixes https://github.com/auth0/socketio-jwt/issues/59 --- example/.gitignore | 2 + example/index.html | 88 +++++++++++++++++++++++++++++++++++++++ example/index.js | 25 +++++++++++ example/package.json | 10 +++++ example/public/index.html | 64 ---------------------------- example/server.js | 54 ------------------------ 6 files changed, 125 insertions(+), 118 deletions(-) create mode 100644 example/.gitignore create mode 100644 example/index.html create mode 100644 example/index.js create mode 100644 example/package.json delete mode 100644 example/public/index.html delete mode 100644 example/server.js diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..a56a7ef --- /dev/null +++ b/example/.gitignore @@ -0,0 +1,2 @@ +node_modules + diff --git a/example/index.html b/example/index.html new file mode 100644 index 0000000..4a7ac34 --- /dev/null +++ b/example/index.html @@ -0,0 +1,88 @@ + + + + Socket.IO chat + + + + + + + + +
+ +
+
+ +
+ +
+
+ + + + + \ No newline at end of file diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..9c7e337 --- /dev/null +++ b/example/index.js @@ -0,0 +1,25 @@ +var app = require('express')(); +var http = require('http').Server(app); +var io = require('socket.io')(http); +var socketioJwt = require('socketio-jwt'); + +app.get('/', function(req, res){ + res.sendFile(__dirname + '/index.html'); +}); + +io + .on('connection', socketioJwt.authorize({ + secret: Buffer('YOUR_CLIENT_SECRET', 'base64'), + timeout: 15000 // 15 seconds to send the authentication message + })) + .on('authenticated', function(socket){ + console.log('connected & authenticated: ' + socket.decoded_token.toString()); + socket.on('chat message', function(msg){ + debugger; + io.emit('chat message', msg); + }); + }); + +http.listen(3001, function(){ + console.log('listening on *:3001'); +}); diff --git a/example/package.json b/example/package.json new file mode 100644 index 0000000..f4ab4e8 --- /dev/null +++ b/example/package.json @@ -0,0 +1,10 @@ +{ + "name": "socket-chat-example", + "version": "0.0.1", + "description": "my first socket.io app", + "dependencies": { + "express": "^4.10.2", + "socket.io": "^1.3.6", + "socketio-jwt": "^4.3.1" + } +} diff --git a/example/public/index.html b/example/public/index.html deleted file mode 100644 index dee5bb6..0000000 --- a/example/public/index.html +++ /dev/null @@ -1,64 +0,0 @@ - - - - Socket.io with JWTs - - -
- - - -
- -
- -
- - - - - - - diff --git a/example/server.js b/example/server.js deleted file mode 100644 index 274635a..0000000 --- a/example/server.js +++ /dev/null @@ -1,54 +0,0 @@ -var express = require('express'); -var http = require('http'); - -var socketIo = require('socket.io'); -var socketio_jwt = require('../'); //require('socketio-jwt'); - -var jwt = require('jsonwebtoken'); -var jwt_secret = 'foo bar big secret'; - -var app = express(); - -var serveStatic = require('serve-static'); -var bodyParser = require('body-parser'); - -app.use(serveStatic('public', {'index': ['index.html']})); -app.use(bodyParser.json()); - -app.post('/login', function (req, res) { - var profile = { - first_name: 'John', - last_name: 'Doe', - email: 'john@doe.com', - id: 123 - }; - - // We are sending the profile inside the token - var token = jwt.sign(profile, jwt_secret, { expiresInMinutes: 60*5 }); - - res.json({token: token}); -}); - -var server = http.createServer(app); -var sio = socketIo.listen(server); - -sio.use(socketio_jwt.authorize({ - secret: jwt_secret, - handshake: true -})); - -sio.sockets - .on('connection', function (socket) { - console.log(socket.decoded_token.email, 'connected'); - socket.on('ping', function (m) { - socket.emit('pong', m); - }); - }); - -setInterval(function () { - sio.sockets.emit('time', Date()); -}, 5000); - -server.listen(9000, function () { - console.log('listening on http://localhost:9000'); -}); \ No newline at end of file