-
Notifications
You must be signed in to change notification settings - Fork 1
/
peersocket.js
131 lines (96 loc) · 3.46 KB
/
peersocket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Based off of Shawn Van Every's Live Web
// http://itp.nyu.edu/~sve204/liveweb_fall2013/week3.html
var Headless = require('./headless')
var minimist = require("minimist")
var argv = minimist(process.argv.slice(2))
var key = argv.cabal || 'cabal://0571a52685ead4749bb7c978c1c64767746b04dcddbca3dc53a0bf6b4cb8f398'
var opts = {}
var cabalkey = key.replace("cabal://", "").replace("cbl://", "")
var headless = Headless(cabalkey, { temp: opts.temp || false })
// HTTP Portion
var http = require('http');
// URL module
var url = require('url');
var path = require('path');
//var Cabal=require('cabal-core')
const port = 8899;
// Using the filesystem module
var fs = require('fs');
var server = http.createServer(handleRequest);
server.listen(port);
console.log('Server started on port '+port);
function handleRequest(req, res) {
// What did we request?
var pathname = req.url;
// If blank let's ask for index.html
if (pathname == '/') {
pathname = '/index.html';
}
// Ok what's our file extension
var ext = path.extname(pathname);
// Map extension to file type
var typeExt = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css'
};
// What is it? Default to plain text
var contentType = typeExt[ext] || 'text/plain';
// User file system module
fs.readFile(__dirname + pathname,
// Callback function for reading
function (err, data) {
// if there is an error
if (err) {
res.writeHead(500);
return res.end('Error loading ' + pathname);
}
// Otherwise, send the data, the contents of the file
res.writeHead(200,{ 'Content-Type': contentType });
res.end(data);
}
);
}
// WebSocket Portion
// WebSockets work with the HTTP server
var io = require('socket.io').listen(server);
// Register a callback function to run when we have an individual connection
// This is run for each individual user that connects
var id;
io.sockets.on('connection',
// We are given a websocket object in our function
function (socket) {
console.log("We have a new client: " + socket.id);
//socket.broadcast.emit('peer',headless.peers() );
console.log("connect to swarm ...");
headless.disconnect();
headless.connect();
headless.id((my_id) => {
id=my_id;
})
headless.onPeerConnected((peerId) => {
//send_packet={ type: "peerConnected", data: peerId};
//console.log(send_packet);
//console.log(`${peerId} connected`)
//console.log('got peers', headless.peers())
console.log('headless.peers(): '+headless.peers());
console.log('local key: '+id);
send_packet={'local_key':id,'peer':peerId};
//headless.post({'channel':'testing','message':'test! saw:'+peerId});
//socket.broadcast.emit('peer',peerId.substring(0,5) );
//socket.broadcast.emit('peer',peerId);
socket.broadcast.emit('peer-connected',send_packet);
});
headless.onMessageReceived((data) => {
//send_packet={ type: "peerConnected", data: peerId};
//console.log(send_packet);
//console.log(`${peerId} connected`)
//console.log('got peers', headless.peers())
console.log('message received: '+data);
//console.log('my id:',headless.id);
//socket.broadcast.emit('peer',peerId.substring(0,5) );
socket.broadcast.emit('message',data);
});
// When this user emits, client side: socket.emit('otherevent',some data);
}
);