123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- function rr(name) {
- console.log("RRR:", name)
- return require(name);
- }
- var util = require("util");
- var config = {};
- try {
- //running in docker / cloudron
- util._extend(config, require("/app/data/config.json"));
- }catch(e){
- console.log("no config present")
- }
- console.log("CONFIG",config);
-
- var express = require("express");
- var app = require('express')();
- var myserver = require('http').Server(app);
- var io = require('socket.io')(myserver);
- var chat = io
- .of('/chat')
- .on('connection', function (socket) {
- socket.emit('a message', {
- that: 'only'
- , '/chat': 'will get'
- });
- chat.emit('a message', {
- everyone: 'in'
- , '/chat': 'will get'
- });
- });
- var news = io
- .of('/news')
- .on('connection', function (socket) {
- console.log("KLLK");
- socket.on("woot",function(){
- console.log("SSSSS",arguments);
- })
- socket.emit('item', { news: 'item' });
- });
- if(config.peers){
- setTimeout(function(){
- var lives = config.peers.map(function(peer){
- var con = require("socket.io-client")('http://'+peer.ip+':'+peer.port+"/chat");
-
- con.on('connect', function () {
- chat.emit('hi!');
- console.log("CHATE",arguments);
- }).on("a message",function(message){
- console.log("cccARG",message);
- });
-
- return {peer:peer,con: con }
- })
- console.log(lives)
- },5000)
- /*
- other_server.on("connect",function(){
- other_server.on('message',function(data){
- // We received a message from Server 2
- // We are going to forward/broadcast that message to the "Lobby" room
- console.log("LOG GOT MESSAGE");
- chat.emit("a message",data);
- });
- });
- */
- }
- function tester(){
- function newsa(){
- news.emit("item",{"title":"titlllklkl","date":new Date(),"body":"jljlkjl"})
- setTimeout(newsa,Math.floor(Math.random()*100)*1000)
- }
- function chata(){
- chat.emit("a message",{"user":"u"+Math.random(),"date":new Date(),"body":"jljlkjl"})
- setTimeout(chata,Math.floor(Math.random()*20)*1000)
- }
- setTimeout(newsa,Math.floor(Math.random()*100)*1000)
- setTimeout(chata,Math.floor(Math.random()*20)*1000)
- }
- tester();
- app.use(express.static("/app/data"));
- app.get("/", function(req, res, next) {
- try {
- var a = require("." + req.url)(req, res, next);
- } catch (e) {
- res.send(e);
- }
- })
- var http = require('http'),
- formidable = require('formidable'),
- fs = require('fs'),
- path = require('path');
- // All your express server code goes here.
- // ...
- // Upload route.
- app.post('/upload', function(req, res) {
- var form = new formidable.IncomingForm();
- form.multiples = true;
- form.parse(req, function(err, fields, afiles) {
- // `file` is the name of the <input> field of type `file`
- console.log(afiles, fields)
- var ffa = afiles.file[0];
- var old_path = ffa.path,
- file_size = ffa.size,
- file_ext = ffa.name.split('.').pop(),
- index = old_path.lastIndexOf('/') + 1,
- file_name = old_path.substr(index),
- new_path = path.join("/app/data", '/uploads/', file_name + '.' + file_ext);
- fs.readFile(old_path, function(err, data) {
- fs.writeFile(new_path, data, function(err) {
- fs.unlink(old_path, function(err) {
- if (err) {
- res.status(500);
- res.json({
- 'success': false
- });
- } else {
- res.status(200);
- res.json({
- 'success': true
- });
- }
- });
- });
- });
- });
- });
- myserver.listen(3000);
-
|