We will now set up a small Threejs example that uses Socket.IO. Socket.IO will be served using the Node.js and Express server that we set up in the last lesson. Our web client will still be bundled using Webpack.
Socket.IO enables real-time, bidirectional and event-based communication between the browser clients and the Node.js server.
In this example, each client will connect and continually broadcast its position and rotation to the server.
The server then continually updates all the connected clients with the positions and rotations of every other connected client.
The scene will show a new cube for each connection, and also show its position and rotation as it changes in real time. Each client will camera.lookAt() their own cube.
./src/server/server.ts
Replace your ./src/server/server.ts with the script below and install Socket.IO
importexpressfrom'express'importpathfrom'path'importhttpfrom'http'import{Server,Socket}from'socket.io'constport:number=3000classApp{privateserver:http.Serverprivateport:numberprivateio:Serverprivateclients:any={}constructor(port:number){this.port=portconstapp=express()app.use(express.static(path.join(__dirname,'../client')))this.server=newhttp.Server(app)this.io=newServer(this.server)this.io.on('connection',(socket:Socket)=>{console.log(socket.constructor.name)this.clients[socket.id]={}console.log(this.clients)console.log('a user connected : '+socket.id)socket.emit('id',socket.id)socket.on('disconnect',()=>{console.log('socket disconnected : '+socket.id)if(this.clients&&this.clients[socket.id]){console.log('deleting '+socket.id)deletethis.clients[socket.id]this.io.emit('removeClient',socket.id)}})socket.on('update',(message:any)=>{if(this.clients[socket.id]){this.clients[socket.id]=message// relaying the complete message}})})setInterval(()=>{this.io.emit('clients',this.clients)},50)}publicStart(){this.server.listen(this.port,()=>{console.log(`Server listening on port ${this.port}.`)})}}newApp(port).Start()
./src/client/client.ts
Replace your ./src/client/client.ts with the script below and install Socket.IO-Client
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"/><metaname="viewport"content="width=device-width, initial-scale=1"/><title>Three.js TypeScript Tutorials by Sean Bradley : https://sbcode.net/threejs</title><style>body{overflow:hidden;margin:0px;}#pingStats{position:absolute;top:60px;left:4px;width:400px;height:400px;pointer-events:none;color:white;font-family:monospace;}</style></head><body><divid="pingStats"></div><scripttype="module"src="bundle.js"></script></body></html>
./package.json
Open ./package.json and replace the dev script with this below.
nodemon : so that it auto restarts Node.js whenever the server script is updated,
concurrently : so that we can run webpack, nodemon and TSC in watch mode using the same terminal. If we didn't use concurrently, then we could instead start the 3 processes individually in their own terminals. Concurrently makes it easier to start multiple processes at the same time.
webpack : This is the same as before. It will use the Webpack Dev Server with the ./src/client/webpack.dev.js configuration.
Replace your ./src/client/webpack.dev.js with the script below.
Note the extra proxy information that tells the Webpack Dev Server where to redirect client requests for /socket.io. The Node.js and Express server will host and start the Socket.IO server on address http://127.0.0.1:3000