integrating basic websocket

This commit is contained in:
Josh Burman
2019-02-28 17:49:46 -05:00
parent 9d4a09b8bd
commit b8282bf009
720 changed files with 1573910 additions and 18 deletions

34
src/server.ts Executable file
View File

@ -0,0 +1,34 @@
import * as express from 'express';
import * as http from 'http';
import * as WebSocket from 'ws';
const app = express();
//initialize a simple http server
const server = http.createServer(app);
//initialize the WebSocket server instance
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws: WebSocket) => {
//connection is up, let's add a simple simple event
ws.on('message', (message: string) => {
//log the received message and send it back to the client
console.log('received: %s', message);
ws.send(`Hello, you sent -> ${message}`);
});
//send immediatly a feedback to the incoming connection
ws.send('Hi there, I am a WebSocket server');
});
// start our server
server.listen(process.env.PORT || 80, () => {
console.log(`Server started :)`);
});
app.get('/', (req, res) => {
res.send('Braid is running!\n');
});