Created
July 31, 2019 18:25
-
-
Save Lightnet/02ba9b336d08f0a520c6f4b166af1ce2 to your computer and use it in GitHub Desktop.
Gun 2 Server Instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// var Gun = require('gun'); // in NodeJS | |
// var Gun = require('gun/gun'); // in React | |
//console.log(location.host+"/gun"); | |
//console.log(window.location.href+'gun'); | |
//console.log('http://localhost:8080/gun'); | |
//console.log(window.location.origin+'/gun'); | |
//var gun = Gun(window.location.origin+'gun'); | |
let gunurl = window.location.origin+'/gun'; | |
//gunurl = 'http://localhost:3000/gun'; | |
//var gun = Gun('http://localhost:8080/gun'); | |
var gun = Gun(gunurl); | |
gun.on('hi', peer => {//peer connect | |
console.log('connect peer to',peer); | |
//console.log('peer connect!'); | |
}); | |
gun.on('bye', (peer)=>{// peer disconnect | |
console.log('disconnected from', peer); | |
//console.log('disconnected from peer!'); | |
}); | |
gun.get('mark').put({ | |
name: "Mark", | |
email: "[email protected]", | |
}); | |
let doc = document.getElementById('gunjs') | |
console.log(doc); | |
console.log("data?"); | |
gun.get('mark').on(function(data, key){ | |
console.log("update:", data); | |
doc.innerText = data.name; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title></title> | |
<meta name="description" content=""> | |
<!--<link id="favicon" rel="icon" href="" type="image/x-icon">--> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<script src="/socket.io/socket.io.js"></script> | |
<script src="/gun.js" defer></script> | |
<script src="/client.js" defer></script> | |
</head> | |
<body> | |
<main> | |
<labe>Polka Test with Gun.js and Socket.io</labe> | |
<div id="gunjs"> | |
... | |
</div> | |
</main> | |
<footer> | |
</footer> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Gun = require("gun"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const http = require("http"); | |
var { PORT = 8080, NODE_ENV } = process.env; | |
const dev = NODE_ENV === "development"; | |
console.log("dev: " + dev); | |
const server = http.createServer(function(request, response) { | |
//console.log("request starting..."); | |
if (Gun.serve(request, response)) {//get gun.js ex. <script src="/gun.js"> | |
return; | |
} // filters gun requests! | |
//handle files as public folder | |
var filePath = "." + request.url; | |
if (filePath === "./") filePath = "./index.html"; | |
var extname = path.extname(filePath); | |
var contentType = "text/html"; | |
switch (extname) { | |
case ".js": | |
contentType = "text/javascript"; | |
break; | |
case ".css": | |
contentType = "text/css"; | |
break; | |
case ".json": | |
contentType = "application/json"; | |
break; | |
case ".png": | |
contentType = "image/png"; | |
break; | |
case ".jpg": | |
contentType = "image/jpg"; | |
break; | |
case ".wav": | |
contentType = "audio/wav"; | |
break; | |
default: | |
contentType = "text/html"; | |
} | |
fs.readFile(filePath, function(error, content) { | |
if (error) { | |
if (error.code === "ENOENT") { | |
fs.readFile("./404.html", function(error, content) { | |
response.writeHead(200, { "Content-Type": contentType }); | |
response.end(content, "utf-8"); | |
}); | |
} else { | |
response.writeHead(500); | |
response.end( | |
"Sorry, check with the site admin for error: " + error.code + " ..\n" | |
); | |
response.end(); | |
} | |
} else { | |
response.writeHead(200, { "Content-Type": contentType }); | |
response.end(content, "utf-8"); | |
} | |
}); | |
}); | |
server.listen(PORT, err => { | |
if (err) throw err; | |
//console.log(app); | |
console.log(`> Running on localhost:`+PORT); | |
}); | |
var gun = Gun({ | |
web: server, | |
file:'data1', | |
}); | |
gun.on('hi', peer => {//peer connect | |
//console.log('connect peer to',peer); | |
console.log('peer connect!'); | |
}); | |
gun.on('bye', (peer)=>{// peer disconnect | |
//console.log('disconnected from', peer); | |
console.log('disconnected from peer!'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Gun = require("gun"); | |
var { PORT = 3000, NODE_ENV } = process.env; | |
const dev = NODE_ENV === "development"; | |
console.log("dev: " + dev); | |
var server = require("http") | |
.createServer() | |
.listen(PORT, err => { | |
if (err) throw err; | |
//console.log(app); | |
console.log(`> Running on localhost:`+PORT); | |
}); | |
var gun = Gun({ | |
web: server, | |
peers:['http://localhost:8080/gun'], | |
file:'data2', | |
}); | |
gun.on('hi', peer => {//peer connect | |
//console.log('connect peer to',peer); | |
console.log('peer connect!'); | |
}); | |
gun.on('bye', (peer)=>{// peer disconnect | |
//console.log('disconnected from', peer); | |
console.log('disconnected from peer!'); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment