Exemplo n.º 1
0
    //function to add project to server
    function startServing(projects) {

        urls = [];
        serverProjects = [];

        _.each(projects, function (project) {

            var projectUrl = '/' + project.config.serverUrl + '/';

            app.use(projectUrl, express.static(project.path));

            app.use(projectUrl, express.directory(project.path, {icons: true}));

            if (!project.config.useCustomServer) {

                serverProjects.push({ name: project.name, url: projectUrl});
            }

            if (project.config.useCustomServer) {

                urls.push(getLiveUrl(project));
            }
        });

        //Send data to browser extensions
        wsServer.broadcast(angular.toJson({urls: urls}));

    }
Exemplo n.º 2
0
    wsServer.on('request', function (request) {

        request.accept('', request.origin);

        wsServer.broadcast(angular.toJson({urls: urls}));

    });
Exemplo n.º 3
0
    refreshServer.on('request', function (request) {

        request.accept('', request.origin);

        refreshServer.broadcast(("!!ver:1.6"));

    });
Exemplo n.º 4
0
	connection.on('message', function(message) {

		if (message.type === 'utf8') {

			// If the user doesn't have a nickname (first message)
			if (!client.nickname) {
				var tmpNick = escapeHtml(trim(message.utf8Data));

				// If the nickname is available and contains less than 15 characters
				if (tmpNick.length <= 15 && getClientIndex(tmpNick) === null) {
					client.nickname = tmpNick;
					client.color = colors.shift();

					clients.push(client);

					wsServer.broadcast(JSON.stringify({ type: 'new_user', data: client }));

					connection.sendUTF(JSON.stringify({ type: 'history', data: history }));
					connection.sendUTF(JSON.stringify({ type: 'user_list', data: clients }));

					console.log((new Date()) + ' - ' + connection.remoteAddress + ' is identified as ' + client.nickname);

				// If the nickname is already in use
				} else {
					connection.sendUTF(JSON.stringify({ type: 'nick_error', data: tmpNick }));
				}

			// If the user is already logged in
			} else {
				console.log((new Date()) + ' - Received the message: ' + message.utf8Data + ', from: ' + client.nickname + ' (' + connection.remoteAddress + ')');

				var data = {
					nickname: client.nickname,
					color: client.color,
					message: escapeHtml(trim(message.utf8Data)),
					time: (new Date()).getTime()
				};

				history.push(data);
				history = history.slice(-20);

				wsServer.broadcast(JSON.stringify({ type: 'message', data: data }));
			}
		}
	});
Exemplo n.º 5
0
	connection.on('close', function(reasonCode, description) {
		console.log((new Date()) + ' - ' + client.nickname + ' (' + connection.remoteAddress + ') disconnected.');

		clients.splice(getClientIndex(client.nickname), 1);
		colors.push(client.color);

		wsServer.broadcast(JSON.stringify({ type: 'remove_user', data: client.nickname }));

	});
Exemplo n.º 6
0
function sendToClient( type,data , connection){
	if ('object' == typeof data ) data.time = new Date

	var dataToClient = JSON.stringify( { type: type, data: data } )
	if (null != connection)
		connection.sendUTF(dataToClient)
	else
		wsServer.broadcast(dataToClient)
	}
Exemplo n.º 7
0
    function refresh(file) {

        var data = JSON.stringify([
            'refresh', {
                path: file,
                apply_js_live: false,
                apply_css_live: true
            }
        ]);

        refreshServer.broadcast(data);
    }
Exemplo n.º 8
0
    // function to add project to server
    function startServing(projects) {

        urls = [];

        _.each(projects, function(project){

            if (!project.config.useCustomServer) {

                app.use('/' + project.id + '/', express.static(project.path));

                app.use('/' + project.id + '/', express.directory(project.path, {icons: true}));
            }

            if (project.config.liveRefresh) {

                urls.push(getLiveUrl(project));
            }

        });

        //Send data to browser extensions
        wsServer.broadcast(angular.toJson({urls: urls}));

    }
Exemplo n.º 9
0
 connection.on('message', function(msg) {
   console.log('WebSocket message.');
   console.log(msg);
   wsServer.broadcast(msg.utf8Data);
 })
Exemplo n.º 10
0
    require('nw.gui').Window.get().on('close', function () {

        wsServer.broadcast(angular.toJson({ urls: []}));

    });
Exemplo n.º 11
0
        //function to add project to server
        function startServing(projects) {

            urls = [];

            _.each(projects, function (project) {

                if(!(project.id in projectsBeingServed)) {

                    portfinder.getPort(function (err, port) {

                        var app = express();
                        var server = app.listen(port);
                        var lServer = new WebSocketServer({
                            httpServer: server,
                            autoAcceptConnections: false
                        });

                        lServer.on('request', function (request) {
                            request.accept('', request.origin);
                            lServer.broadcast("!!ver:1.6");
                        });

                        projectsBeingServed[project.id] = {
                            name: project.name,
                            port: port,
                            app: app,
                            server: server,
                            lServer: lServer
                        };

                        projectsBeingServed[project.id].url = getLiveUrl(project);

                        app.get('/livereload.js', function(req, res) {
                            res.sendfile(config.basePath + '/vendor/livereload.js');
                        });

                        app.get('/prepros.js', function(req, res) {

                            var snippet = '' +
                                '(function(){' +
                                '   try {' +
                                    '    var script = document.createElement("script");' +
                                    '    script.src="/livereload.js?snipver=1&host=" + window.location.hostname + "&port=" + window.location.port;' +
                                    '    document.querySelectorAll("body")[0].appendChild(script);' +
                                    '} catch(e) {}' +
                                '})();';
                            res.setHeader('Content-type', 'application/x-javascript');
                            res.end(snippet);
                        });

                        app.use(liveReloadMiddleWare);
                        app.use(express.static(project.path));
                        app.use(express.directory(project.path, {icons: true}));

                    });
                }

                if(project.config.useCustomServer) {

                    var parsed = url.parse(project.config.customServerUrl);

                    urls.push(parsed.protocol + '//' + parsed.host + '|' + project.id);
                }
            });

            //Send data to browser extensions
            wsServer.broadcast(angular.toJson({urls: urls}));

        }