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

client side socket code for step-2 backend #19

Open
wants to merge 3 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
79 changes: 79 additions & 0 deletions client/js/socket-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// prompts for roomname and username
var roomname = ''
while (roomname === '') {
roomname = prompt('Please enter your roomname')
}

var username = ''
while (username === '') {
username = prompt('please enter your username')
}
// socket connection to server
var socket = io()
console.log('connected client')
// join event emitter
socket.emit('join', roomname)

// connectToRoom listner
socket.on('connectToRoom', function (data) {
document.body.innerHTML = ''
document.write(data)
})

// listner for new player joining
socket.on('joinplayer', (data) => {
console.log(data)
})

// setUsername event emitter
socket.emit('setUsername', { username: username, roomname: roomname })

// userSet listner
socket.on('userSet', function (data) {
const user = data.username
document.body.innerHTML = ''
console.log(user)
document.write(user)
})

// userExists listner
socket.on('userExists', function (data) {
document.body.innerHTML = ''
document.write(data)
})

// newPrompt for new username listner
socket.on('newPrompt', () => {
username = prompt('username taken please enter new username')
socket.emit('setUsername', { username: username, roomname: roomname })// again emitting setUsername
})

// game start event emitter
socket.emit('gameStart', { roomname: roomname })

// card to hand deck event emitter
socket.emit('cardHandToDeck', { cards: 'cards', roomname: roomname })

// provides usernames list when pressed start game button
socket.on('usersList', (data) => {
data.forEach(name => {
console.log(name)
})
})

// less users to start game event
socket.on('lessPlayers', (data) => {
console.log(data)
})

// endGame event emitter
socket.emit('endgame', { roomname: roomname })

// redirect event listner
socket.on('redirect', (data) => {
window.location.href = data
})
// disconnect listner`
socket.on('disconnect', () => {
console.log('user disconnected client')
})
10 changes: 10 additions & 0 deletions client/test.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>

<head>
<title>Hello world</title>
</head>
<script src="/socket.io/socket.io.js"></script>
<body></body>
<script src = './js/socket-client.js'></script>
</html>