Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Jabis Sev贸n
/
gun-multiserver
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
d2d856d0
authored
2020-04-15 18:26:17 +0000
by
Glitch (hello-express)
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Checkpoint
./server.js:13690106/2267
1 parent
1dd53103
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
19 deletions
server.js
server.js
View file @
d2d856d
...
...
@@ -4,31 +4,29 @@ const http = require('http');
const
WebSocket
=
require
(
'ws'
);
var
server
=
http
.
createServer
().
listen
(
3000
);
var
gun1
=
Gun
({
peers
:[],
ws
:
{
noServer
:
true
}});
var
gun2
=
Gun
({
peers
:[],
ws
:
{
noServer
:
true
}});
// LRU with last used sockets
const
QuickLRU
=
require
(
'quick-lru'
);
const
lru
=
new
QuickLRU
({
maxSize
:
100
});
var
wss_event
=
new
WebSocket
.
Server
({
noServer
:
true
});
server
.
on
(
'upgrade'
,
function
(
request
,
socket
,
head
)
{
var
pathname
=
url
.
parse
(
request
.
url
).
pathname
;
var
gun
if
(
lru
.
has
(
pathname
)){
}
else
{
var
gun
=
Gun
({
peers
:[],
ws
:
{
noServer
:
true
}});
lru
.
set
(
pathname
,
gun
);
server
.
on
(
'upgrade'
,
async
function
(
request
,
socket
,
head
)
{
var
pathname
=
url
.
parse
(
request
.
url
).
pathname
||
'/gun'
;
var
gun
=
false
;
if
(
pathname
){
if
(
lru
.
has
(
pathname
)){
// Existing Node
gun
=
await
lru
.
get
(
pathname
);
}
else
{
// Create Node
gun
=
await
Gun
({
peers
:[],
ws
:
{
noServer
:
true
}});
lru
.
set
(
pathname
,
gun
);
}
}
if
(
pathname
===
'/gun1'
)
{
gun1
.
handleUpgrade
(
request
,
socket
,
head
,
function
(
ws
)
{
gun1
.
emit
(
'connection'
,
ws
);
});
}
else
if
(
pathname
===
'/gun2'
)
{
gun2
.
handleUpgrade
(
request
,
socket
,
head
,
function
(
ws
)
{
gun2
.
emit
(
'connection'
,
ws
);
if
(
gun
){
// Handle Request
gun
.
handleUpgrade
(
request
,
socket
,
head
,
function
(
ws
)
{
gun
.
emit
(
'connection'
,
ws
);
});
}
else
{
socket
.
destroy
();
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment