Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions websocket-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ let isServerLocal = true;
let ws_host = 'smartgeometry.herokuapp.com';
let ws_port = '80';
if (isServerLocal == true) {
let ws_host = 'localhost';
let ws_port = '3000';
ws_host = 'localhost';
ws_port = process.env.PORT || 3000;;
}
const options = { constructor: Html5WebSocket };
const rws = new ReconnectingWebSocket('ws://' + ws_host + ':' + ws_port + '/ws', undefined, options);
Expand All @@ -26,8 +26,10 @@ rws.addEventListener('open', () => {
console.log('[Client] Connection to WebSocket server was opened.');
rws.send('Hello, message from client');

setTimeout(function() {
rws.send(JSON.stringify({ method: 'set-background-color', params: { 'color': 'blue' } }));
setTimeout(function () {
rws.send(JSON.stringify(
{ method: 'set-background-color', params: { 'color': 'blue' } }
));
}, 3000);
});

Expand Down Expand Up @@ -61,10 +63,10 @@ rws.onerror = (err) => {
// ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝ ╚══════╝╚══════╝╚═╝ ╚═╝╚══════╝

let handlers = {
"client-id": function(m) {
"client-id": function (m) {
console.log('[Client] Your id is ' + m.params.id);
},
'set-background-color': function(m) {
'set-background-color': function (m) {
// Connect your WebSocket client to smartgeometry.herokuapp.com:80 server
// and go to nono.ma/teach to test this action
console.log('[Client] Set background color to ' + m.params.color);
Expand Down
60 changes: 59 additions & 1 deletion websocket-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 11 additions & 14 deletions websocket-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@
* using https://github.com/websockets/ws
*/

//'use strict';

const express = require('express');
const WebSocket = require('ws');
const SocketServer = require('ws').Server;
const path = require('path');
import express from 'express';
import { WebSocket, WebSocketServer } from 'ws';
import { v4 as uuidv4 } from 'uuid';

const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');

const server = express()
.use((req, res) => res.sendFile(INDEX))
.listen(PORT, () => console.log(`Listening on ${ PORT }`));

const wss = new SocketServer({ server });
const wss = new WebSocketServer({ server });

wss.on('connection', (ws) => {

console.log('Client connected');
const uuid = uuidv4();

console.log(`[${uuid}] Client connected`);

ws.on('close', () => console.log('Client disconnected'));
ws.on('close', () => console.log(`[${uuid}] Client disconnected`));

ws.on('message', function incoming(message) {
ws.on('message', (message) => {

console.log('[Server] Received message: %s', message);
console.log(`[${uuid}] Received message: ${message}`);

// Broadcast to everyone else.
wss.clients.forEach(function each(client) {
wss.clients.forEach((client) => {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(message);
}
Expand Down
Loading