Browse Source

integrating socket.io

master
parent
commit
0d1cfd2e1b
3 changed files with 21 additions and 5 deletions
  1. +6
    -1
      index.html
  2. +9
    -0
      index.js
  3. +6
    -4
      package.json

+ 6
- 1
index.html View File

@ -19,5 +19,10 @@
<ul id="messages"></ul> <ul id="messages"></ul>
<form id="form" action=""> <form id="form" action="">
<input id="input" autocomplete="off" /><button>Send</button> <input id="input" autocomplete="off" /><button>Send</button>
</form> </body>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
</body>
</html> </html>

+ 9
- 0
index.js View File

@ -2,11 +2,20 @@ const express = require('express');
const app = express(); const app = express();
const http = require("http"); const http = require("http");
const server = http.createServer(app); const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html'); res.sendFile(__dirname + '/index.html');
}); });
io.on('connection', (socket) => {
console.log('a new user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
server.listen(3030, () => { server.listen(3030, () => {
console.log('listening on beaglebone.local:3030'); console.log('listening on beaglebone.local:3030');
}); });


+ 6
- 4
package.json View File

@ -1,6 +1,8 @@
{ {
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {}
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"socket.io": "^4.4.1"
}
} }

Loading…
Cancel
Save