Skip to content

Commit 0a6e5cb

Browse files
author
chrisMYchen
committed
Client and Server working
1 parent a82b1b3 commit 0a6e5cb

File tree

321 files changed

+52821
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

321 files changed

+52821
-0
lines changed

Client.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
const WebSocket = require('ws');
3+
4+
5+
class Client {
6+
constructor(serverUrl, key) {
7+
//TODO: URL validation
8+
this.serverUrl = serverUrl;
9+
this.ws = undefined;
10+
this.key = key;
11+
this.opened = false;
12+
}
13+
14+
connect() {
15+
if (this.ws === undefined) {
16+
this.ws = new WebSocket(this.serverUrl, {key: this.key});
17+
console.log("new connection!");
18+
return this.ws;
19+
}
20+
else {
21+
console.log("else statement");
22+
return this.ws;
23+
}
24+
}
25+
26+
sendMessageToServer(data){
27+
if (this.opened) {
28+
console.log("already opened");
29+
this.ws.send(JSON.stringify(data));
30+
}
31+
else {
32+
console.log("ahhhh");
33+
this.ws.on('open', function open() {
34+
this.send(JSON.stringify(data));
35+
});
36+
}
37+
}
38+
39+
40+
open() {
41+
let myWS = this;
42+
this.ws.on('open', function open(openevent) {
43+
myWS.opened = true;
44+
console.log('opened');
45+
console.log(openevent);
46+
});
47+
}
48+
49+
logResponseEvents(){
50+
this.ws.on('message', function incoming(data, flags) {
51+
console.log(data);
52+
});
53+
}
54+
55+
close() {
56+
if (this.ws !== undefined) {
57+
this.ws.close();
58+
this.ws = undefined;
59+
}
60+
}
61+
62+
}
63+
64+
module.exports = Client;
65+
// var myRippledClient = new Client('wss://s1.ripple.com:443');
66+
// myRippledClient.connect();
67+
// const closedLedgerData = {
68+
// "id": "Example watch for new validated ledgers",
69+
// "command": "subscribe",
70+
// "streams": ["ledger"]
71+
// };
72+
// myRippledClient.subscribeToEvents(closedLedgerData);
73+
// myRippledClient.listenEvents();
74+
//
75+
function sleep (time) {
76+
return new Promise((resolve) => setTimeout(resolve, time));
77+
}
78+
79+
80+
81+
let myClient = new Client('ws://localhost:8080', 'Christopher');
82+
myClient.connect();
83+
myClient.open();
84+
myClient.logResponseEvents();
85+
myClient.sendMessageToServer("test data");
86+
sleep(500).then(() => {
87+
console.log("sleeped");
88+
myClient.sendMessageToServer("second slow message wow");
89+
});

RippleNode.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
const WebSocket = require('ws');
3+
const express = require('express');
4+
const http = require('http');
5+
const Server = require('./Server');
6+
const Client = require('./Client');
7+
class RippleNode {
8+
constructor(myPort, id){
9+
this.port = myPort;
10+
this.id = id;
11+
this.server = new Server(this.port);
12+
this.connections = [];
13+
}
14+
//list of urls
15+
connect(urls) {
16+
urls.forEach(function createClientConnection(url) {
17+
let myClient = new Client(url, id);
18+
connections.push(myClient);
19+
});
20+
}
21+
22+
//when node acts as client
23+
logAllResponses() {
24+
this.connections.forEach(function listen(client) {
25+
client.logResponseEvents();
26+
})
27+
}
28+
29+
30+
}
31+
32+
33+
var argv = require('minimist')(process.argv.slice(2));
34+
console.dir(argv);

Server.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
const WebSocket = require('ws');
3+
const express = require('express');
4+
const http = require('http');
5+
6+
class Server {
7+
constructor(myPort) {
8+
this.port = myPort;
9+
this.server = new WebSocket.Server({port: this.port});
10+
}
11+
12+
listen() {
13+
let my_obj = this;
14+
this.server.on('connection', function connection(ws) {
15+
ws.on('message', function incoming(message) {
16+
my_obj.processMessage(message);
17+
// console.log(ws);
18+
my_obj.broadcast("new client connected!");
19+
});
20+
my_obj.sendMessage('hello!', ws);
21+
});
22+
}
23+
24+
processMessage(message) {
25+
//based on message type, we do something different
26+
console.log('received: %s', message);
27+
}
28+
29+
sendMessage(message, ws) {
30+
ws.send(message);
31+
}
32+
33+
broadcast(message) {
34+
this.server.clients.forEach(function each(client) {
35+
if (client.readyState === WebSocket.OPEN) {
36+
client.send(message);
37+
}
38+
});
39+
}
40+
};
41+
42+
module.exports = Server;
43+
//
44+
let myServer = new Server(8080);
45+
myServer.listen();

0 commit comments

Comments
 (0)