-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from sarvasvaKhare/server
Server side code for socket.io step-2 backend
- Loading branch information
Showing
7 changed files
with
144 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
//importing app | ||
const app = require('./app') | ||
const http = require('http') | ||
const socket = require('socket.io') | ||
|
||
//port as enviroment variable | ||
const port = process.env.PORT || 3000 | ||
const server = http.createServer(app) | ||
|
||
//server up check | ||
app.listen(port, () => { | ||
server.listen(port, () => { | ||
console.log('Server is up on port ' + port) | ||
}) | ||
|
||
//socket setup | ||
const io = socket(server) | ||
module.exports = { io } | ||
require('./sockets/socket-server') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,47 @@ | ||
//router setup | ||
// router setup | ||
const express = require('express') | ||
const router = new express.Router() | ||
|
||
//authentication setup | ||
const passport = require("passport"); | ||
// authentication setup | ||
const passport = require('passport') | ||
|
||
//index route | ||
// index route | ||
router.get('', (req, res) => { | ||
res.render('index') | ||
res.redirect('/rooms') | ||
}) | ||
|
||
//google auth entry route | ||
router.get('/login',(req,res)=>{ | ||
res.redirect('/auth/google') | ||
// google auth entry route | ||
router.get('/login', (req, res) => { | ||
res.redirect('/auth/google') | ||
}) | ||
|
||
//route authenticating user by google | ||
// route authenticating user by google | ||
router.get('/auth/google', | ||
passport.authenticate('google',{scope: ["profile"]}) | ||
passport.authenticate('google', { | ||
scope: ['profile'] | ||
}) | ||
) | ||
|
||
//route redirected to after sucessfull authentication from google | ||
router.get('/auth/google/home',passport.authenticate('google', { failureRedirect: "/login" }), | ||
function(req, res) { | ||
res.redirect("/result"); | ||
}); | ||
// route redirected to after sucessfull authentication from google | ||
router.get('/auth/google/home', passport.authenticate('google', { | ||
failureRedirect: '/login' | ||
}), | ||
function (req, res) { | ||
res.redirect('/result') | ||
}) | ||
|
||
// redirected route to render result page | ||
router.get('/result',(req,res)=>{ | ||
if (req.isAuthenticated()){ | ||
router.get('/result', (req, res) => { | ||
if (req.isAuthenticated()) { | ||
res.render('index') | ||
}else{ | ||
res.redirect('/login') | ||
} | ||
|
||
|
||
} else { | ||
res.redirect('/login') | ||
} | ||
}) | ||
|
||
//exporting module for app.js | ||
module.exports = router | ||
// route for socket testing | ||
router.get('/rooms', (req, res) => { | ||
res.render('test') | ||
}) | ||
// exporting module for app.js | ||
module.exports = { router } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const {io} = require('./../index') | ||
|
||
// on connecting with client socket | ||
io.on('connection', (socket) => { | ||
console.log('connected') | ||
// join event listner | ||
socket.once('join', function (roomname) { | ||
let people = io.nsps['/'].adapter.rooms[roomname] | ||
// checks for maximum users and if game started? | ||
if ((people && people.length > 11) || (people && people.allowed != undefined)) { | ||
} else { | ||
socket.join(roomname) // if passed checks joins room | ||
socket.broadcast.emit('broadcast', 'new player joined'); // brodcast every other a player has joined | ||
socket.emit('connectToRoom', "You are in room " + roomname) // emit user the roomname | ||
} | ||
}) | ||
|
||
// set username event listner | ||
socket.on('setUsername', function (data) { | ||
let usernameTaken = 0; // var for checking unique username | ||
io.in(data.roomname).clients((error, clients) => { // getting all clients in room | ||
if (error) throw error; | ||
// loop for checking username of every other player with given username | ||
for (var i = 0; i < clients.length; i++) { | ||
const user = io.sockets.sockets[clients[i]]; // saving socket into user | ||
|
||
if (user.username == data.username) { // if equal therfore username taken | ||
usernameTaken = 1; // setting var to 1 | ||
break; // break from loop | ||
} | ||
} | ||
|
||
if (!usernameTaken) { | ||
socket.username = data.username // setting username in socket of that client | ||
socket.emit('userSet', { username: data.username }) // emiting username to client side | ||
} else { | ||
// emitting dupilicate username to client side | ||
socket.emit('userExists', data.username + ' username is taken! Try some other username.'); | ||
socket.emit('newPrompt'); //emitting new Prompt for new username | ||
} | ||
}) | ||
}); | ||
|
||
// gameStart listner | ||
socket.on('gameStart', (data) => { | ||
let usernames = []; // array for storing usernames | ||
let people = io.nsps['/'].adapter.rooms[data.roomname] | ||
io.in(data.roomname).clients((error, clients) => { // getting all clients in a room | ||
if (error) throw error; | ||
clients.forEach(client => { | ||
let person = io.sockets.sockets[client] | ||
usernames.push(person.username) // adding their username to usernames array | ||
}); | ||
}) | ||
if (people.length > 1) { | ||
people.allowed = 1; // setting var for game started true | ||
io.in(data.roomname).emit('usersList', usernames) // emitting usernames list to clients | ||
} else { | ||
io.in(data.roomname).emit('lessPlayers', 'need more to start game') | ||
} | ||
|
||
}) | ||
// cardHandToDeck event listner | ||
socket.on('cardHandToDeck', (data) => { | ||
io.in(data.roomname).emit('cardsInDeck', data.ids); | ||
}) | ||
// endGame event listner | ||
socket.on('endGame',(data)=>{ | ||
io.in(data.roomname).emit('redirect','/') | ||
}) | ||
// dissconection event | ||
socket.once('disconnect', function () { | ||
console.log('disconnected') | ||
}) | ||
}) | ||
|