Skip to content

Commit 6c5d22f

Browse files
author
gamontec
committed
tutorial 3 wip
1 parent d999d19 commit 6c5d22f

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

03-multiplayer/multiplayer.md

+91-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 03 – Intro to Three.js – Multiplayer
2-
2+
## Client
33
```js
44
// 03 – Intro to Three.js – Multplayer
55
// Client code
@@ -234,4 +234,94 @@ function sendMessage(data){
234234
socket.send(data)
235235
}
236236
}
237+
```
238+
## Server
239+
```js
240+
// 03 – Intro to Three.js – Multplayer
241+
// Server code
242+
// by Guillermo Montecinos
243+
// March 2021
244+
// WebSocket implementetion based on Tom Igoe's web socket examples: https://tigoe.github.io/websocket-examples/
245+
246+
const express = require('express')
247+
const path = require('path')
248+
249+
// Instantiate express app
250+
const app = express()
251+
// Import and intialize ws server instance on express
252+
const wsServer = require('express-ws')(app)
253+
254+
let users = []
255+
// Use the public folder to load html/js files
256+
app.use(express.static('public'))
257+
258+
// on get '/' send page to the user
259+
app.get('/', function (req, res){
260+
res.sendFile(path.join(__dirname, '/public/index.html'))
261+
})
262+
263+
// Callback function that get's executed when a new socket is intialized/connects
264+
function handleWs(ws){
265+
console.log('New user connected: ' + ws)
266+
// As soon as a new client connects, assign them an id, store it in the users array and send it back to the client
267+
ws.send(JSON.stringify({type: 'user-init', id: users.length}))
268+
users.push({socket: ws, id: users.length})
269+
270+
// When a user disconnects, remove it from the users array and inform all the clients in the network
271+
function endUser() {
272+
const index = users.findIndex(user => user.socket == ws)
273+
users.forEach((user) => {
274+
if(user.socket != ws) {
275+
// Let know all users that aren't the one disconnecting from the disconnection
276+
user.socket.send(JSON.stringify({type: 'user-disconnect', id: users[index].id}))
277+
}
278+
})
279+
console.log('user id: ' + users[index].id + ' disconnected')
280+
users.splice(index, 1)
281+
}
282+
// This callback is triggered everytime a new message is received
283+
function messageReceived(m){
284+
// Parse de data to json
285+
const data = JSON.parse(m)
286+
// Data setup means a new user received their id and sends back all the initialization parameters
287+
if(data.type == 'user-setup') {
288+
// Broadcast user setup message called new-user to setup new user in all users except from the originary
289+
users.forEach((user) => {
290+
// If the user correpsonds to the one on setup, store its initialization data
291+
if(user.socket == ws) {
292+
user.color = data.color
293+
user.matrix = data.matrix
294+
}
295+
// If there are users different to the one setting up, it means there were users previously connected. Hence, we have to let the new user know of their existance.
296+
else {
297+
// Send to the new user the previous users data
298+
ws.send(JSON.stringify({type: 'previous-user', id: user.id, color: user.color, matrix: user.matrix}))
299+
// Send to other users the new user setup
300+
data.type = 'new-user'
301+
user.socket.send(JSON.stringify(data))
302+
}
303+
})
304+
}
305+
else if(data.type == 'user-update') {
306+
// When a user udpates its position, let all other users about it.
307+
users.forEach((user) => {
308+
if(user.socket != ws) {
309+
user.socket.send(JSON.stringify({type: 'user-move', matrix: data.matrix, id: data.id}))
310+
}
311+
})
312+
}
313+
}
314+
// Attach callbacks to the socket as soon it gets connected
315+
ws.on('message', messageReceived)
316+
ws.on('close', endUser)
317+
}
318+
319+
// Server init
320+
const port = process.env.PORT || 3000
321+
app.listen(port, function(){
322+
console.log('Server listening on port ' + port)
323+
})
324+
325+
// Sockets init
326+
app.ws('/', handleWs)
237327
```

0 commit comments

Comments
 (0)