Skip to content

Commit 4813c12

Browse files
committed
Update dependencies and modernize example a bit
- refactor for on generic event - camel case events "chat message" -> "chatMessage" - define event once and reference everywhere for both client and server - audix fix
1 parent da62792 commit 4813c12

20 files changed

+531
-16
lines changed

server/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
<script>
3838
var socket = io();
3939
$('form').submit(function(){
40-
socket.emit('chat message', $('#m').val());
40+
socket.emit('chatMessage', $('#m').val());
4141
$('#m').val('');
4242
return false;
4343
});
44-
socket.on('chat message', function(msg){
44+
socket.on('chatMessage', function(msg){
4545
$('#messages').append($('<li>').text(msg));
4646
window.scrollTo(0,document.body.scrollHeight);
4747
});

server/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,41 @@ const http = require('http').Server(app);
33
const io = require('socket.io')(http);
44
const util = require('util');
55
const port = 3000;
6-
const clients = [];
6+
const clients = []; //track connected clients
77

88
//Server Web Client
99
app.get('/', function(req, res){
1010
res.sendFile(__dirname + '/index.html');
1111
});
1212

13+
//make one reference to event name so it can be easily renamed
14+
const chatEvent = "chatMessage";
15+
1316
//When a client connects, bind each desired event to the client socket
14-
io.on('connection', function(socket){
17+
io.on('connection', socket =>{
1518
//track connected clients via log
1619
clients.push(socket.id);
17-
let clientConnectedMsg = 'User connected ' + util.inspect(socket.id) + ', total: ' + clients.length;
18-
io.emit('chat message', clientConnectedMsg);
20+
const clientConnectedMsg = 'User connected ' + util.inspect(socket.id) + ', total: ' + clients.length;
21+
io.emit(chatEvent, clientConnectedMsg);
1922
console.log(clientConnectedMsg);
2023

2124
//track disconnected clients via log
22-
socket.on('disconnect', function(){
25+
socket.on('disconnect', ()=>{
2326
clients.pop(socket.id);
24-
let clientDisconnectedMsg = 'User disconnected ' + util.inspect(socket.id) + ', total: ' + clients.length;
25-
io.emit('chat message', clientDisconnectedMsg);
27+
const clientDisconnectedMsg = 'User disconnected ' + util.inspect(socket.id) + ', total: ' + clients.length;
28+
io.emit(chatEvent, clientDisconnectedMsg);
2629
console.log(clientDisconnectedMsg);
2730
})
2831

2932
//multicast received message from client
30-
socket.on('chat message', function(msg){
31-
let combinedMsg = socket.id.substring(0,4) + ': ' + msg;
32-
io.emit('chat message', combinedMsg);
33+
socket.on(chatEvent, msg =>{
34+
const combinedMsg = socket.id.substring(0,4) + ': ' + msg;
35+
io.emit(chatEvent, combinedMsg);
3336
console.log('multicast: ' + combinedMsg);
3437
});
3538
});
3639

3740
//Start the Server
38-
http.listen(port, function(){
41+
http.listen(port, () => {
3942
console.log('listening on *:' + port);
4043
});

server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.2.0",
44
"description": "basic chat example server with web client",
55
"dependencies": {
6-
"express": "4.10.2",
6+
"express": "^4.17.1",
77
"socket.io": "^2.0.0"
88
}
99
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Default bindings for Gamepads",
3+
"controller_type": "gamepad",
4+
"last_edited_by": "UnrealEngine",
5+
"bindings":
6+
{
7+
"/actions/main":
8+
{
9+
"sources": []
10+
}
11+
},
12+
"description": ""
13+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "Default bindings for MixedReality",
3+
"controller_type": "holographic_controller",
4+
"last_edited_by": "UnrealEngine",
5+
"bindings":
6+
{
7+
"/actions/main":
8+
{
9+
"sources": [],
10+
"poses": [
11+
{
12+
"output": "/actions/main/in/controllerleft",
13+
"path": "/user/hand/left/pose/raw",
14+
"requirement": "optional"
15+
},
16+
{
17+
"output": "/actions/main/in/controllerright",
18+
"path": "/user/hand/right/pose/raw"
19+
}
20+
],
21+
"skeleton": [
22+
{
23+
"output": "/actions/main/in/skeletonleft",
24+
"path": "/user/hand/left/input/skeleton/left"
25+
},
26+
{
27+
"output": "/actions/main/in/skeletonright",
28+
"path": "/user/hand/right/input/skeleton/right"
29+
}
30+
],
31+
"haptics": [
32+
{
33+
"output": "/actions/main/out/vibrateleft",
34+
"path": "/user/hand/left/output/haptic"
35+
},
36+
{
37+
"output": "/actions/main/out/vibrateright",
38+
"path": "/user/hand/right/output/haptic"
39+
}
40+
]
41+
}
42+
},
43+
"description": ""
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Default bindings for Valve Index Headset",
3+
"controller_type": "indexhmd",
4+
"last_edited_by": "UnrealEngine",
5+
"bindings":
6+
{
7+
"/actions/main":
8+
{
9+
"sources": []
10+
}
11+
},
12+
"description": ""
13+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "Default bindings for ValveIndex",
3+
"controller_type": "knuckles",
4+
"last_edited_by": "UnrealEngine",
5+
"bindings":
6+
{
7+
"/actions/main":
8+
{
9+
"sources": [],
10+
"poses": [
11+
{
12+
"output": "/actions/main/in/controllerleft",
13+
"path": "/user/hand/left/pose/raw",
14+
"requirement": "optional"
15+
},
16+
{
17+
"output": "/actions/main/in/controllerright",
18+
"path": "/user/hand/right/pose/raw"
19+
}
20+
],
21+
"skeleton": [
22+
{
23+
"output": "/actions/main/in/skeletonleft",
24+
"path": "/user/hand/left/input/skeleton/left"
25+
},
26+
{
27+
"output": "/actions/main/in/skeletonright",
28+
"path": "/user/hand/right/input/skeleton/right"
29+
}
30+
],
31+
"haptics": [
32+
{
33+
"output": "/actions/main/out/vibrateleft",
34+
"path": "/user/hand/left/output/haptic"
35+
},
36+
{
37+
"output": "/actions/main/out/vibrateright",
38+
"path": "/user/hand/right/output/haptic"
39+
}
40+
]
41+
}
42+
},
43+
"description": ""
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "Default bindings for OculusTouch",
3+
"controller_type": "oculus_touch",
4+
"last_edited_by": "UnrealEngine",
5+
"bindings":
6+
{
7+
"/actions/main":
8+
{
9+
"sources": [],
10+
"poses": [
11+
{
12+
"output": "/actions/main/in/controllerleft",
13+
"path": "/user/hand/left/pose/raw",
14+
"requirement": "optional"
15+
},
16+
{
17+
"output": "/actions/main/in/controllerright",
18+
"path": "/user/hand/right/pose/raw"
19+
}
20+
],
21+
"skeleton": [
22+
{
23+
"output": "/actions/main/in/skeletonleft",
24+
"path": "/user/hand/left/input/skeleton/left"
25+
},
26+
{
27+
"output": "/actions/main/in/skeletonright",
28+
"path": "/user/hand/right/input/skeleton/right"
29+
}
30+
],
31+
"haptics": [
32+
{
33+
"output": "/actions/main/out/vibrateleft",
34+
"path": "/user/hand/left/output/haptic"
35+
},
36+
{
37+
"output": "/actions/main/out/vibrateright",
38+
"path": "/user/hand/right/output/haptic"
39+
}
40+
]
41+
}
42+
},
43+
"description": ""
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Default bindings for Rift Headset",
3+
"controller_type": "rift",
4+
"last_edited_by": "UnrealEngine",
5+
"bindings":
6+
{
7+
"/actions/main":
8+
{
9+
"sources": []
10+
}
11+
},
12+
"description": ""
13+
}

0 commit comments

Comments
 (0)