Custom PHP Page


  • Community Rep

    console.log is just a print statement, like echo or print() in php.



  • oh nice, how send it as var from plugin to template? becouse if i add this script as script won't be secure becouse i will show my user password to everyone who insepct the code



  • @belstgut said:

    var TeamSpeakClient = require("node-teamspeak"),
        util = require("util");
    
    var cl = new TeamSpeakClient("##SERVERIP###");
    cl.send("login", {client_login_name: "##USERNAME##", client_login_password: "##PASSWORD##"}, function(err, response, rawResponse){
        cl.send("use", {sid: 1}, function(err, response, rawResponse){
            cl.send("clientinfo", {clid: "##CLIENTID##"} ,function(err, response, rawResponse){
                console.log(util.inspect(response));
            });
        });
    });
    

    this should connect you to your server, log u in, select virtual server 1, and print clientinfo for the client with id ##CLIENTID##.

    (slightly modified from the node-teamspeak example)

    tryed to use it between <script> tang on HTML and nothing appeared


  • Community Rep

    That code would go in you server js.

    Example:

    (function(module) {
    	"use strict";
    
    	var TeamSpeakClient = require("node-teamspeak"),
    		util = require("util");
    
    	var Plugin = {};
    
    	// static:app.load
    	Plugin.init = function(params, callback) {
    		var app = params.router,
    			middleware = params.middleware,
    			controllers = params.controllers;
    
    		var cl = new TeamSpeakClient("##SERVERIP###");
    		cl.send("login", {client_login_name: "##USERNAME##", client_login_password: "##PASSWORD##"}, function(err, response, rawResponse){
    			cl.send("use", {sid: 1}, function(err, response, rawResponse){
    				cl.send("clientinfo", {clid: "##CLIENTID##"} ,function(err, response, rawResponse){
    					console.log(util.inspect(response));
    				});
    			});
    		});
    
    		callback();
    	};
    
    	module.exports = Plugin;
    }(module));
    

    To talk between, use Sockets like so:

    (function(module) {
    	"use strict";
    
    	var TeamSpeakClient = require("node-teamspeak"),
    		util = require("util");
    
    	var SocketPlugins = module.parent.require('./socket.io/plugins');
    
    	var Plugin = {};
    
    	// static:app.load
    	Plugin.init = function(params, callback) {
    		var app = params.router,
    			middleware = params.middleware,
    			controllers = params.controllers;
    
    		// Define a namespace for your socket listener
    		SocketPlugins.YourPlugin = {
    
    			// Define the listener
    			formSubmit: function (socket, data, next) {
    
    				// data is your form results, as an array.
    				console.log(data[0].value); // Prints the value of the first item in your form.
    
    				var cl = new TeamSpeakClient("##SERVERIP###");
    				cl.send("login", {client_login_name: "##USERNAME##", client_login_password: "##PASSWORD##"}, function(err, response, rawResponse){
    					cl.send("use", {sid: 1}, function(err, response, rawResponse){
    						cl.send("clientinfo", {clid: "##CLIENTID##"} ,function(err, response, rawResponse){
    
    							console.log(util.inspect(response));
    
    							// Send a response back to the client.
    							next(null, {whatever: datayouwant});
    						});
    					});
    				});
    			}
    		};
    		
    
    		callback();
    	};
    
    	module.exports = Plugin;
    }(module));
    

    client js:

    <form>...</form>
    
    <script>
    $( "form" ).submit(function( event ) {
      socket.emit( 'plugins.YourPlugin.formSubmit', $( this ).serializeArray(), function (err, data) {
        if (data) console.log("Got response from server.");
      });
      event.preventDefault();
    });
    </script>
    

    Check out the socket.io docs and jquery docs.



  • How to send the results in a div or something?


  • Community Rep

    @DavidPS You want to use JQuery selectors.

    Something like this.

    http://jsfiddle.net/nf8vs5uv/



  • Nice I nearly finished it! just need how to store some info to database and how to display errors with the nodebb modal boxes



  • More questions: How make content for registred users only?


  • Community Rep

    @DavidPS

    On the server, in your socket function, socket.uid is the user's identifier, and it is 0 (false) for guests. So, just return an error when it's 0.

    formSubmit: functio6n (socket, data, next) {
        if (!socket.uid) return next(true);
    
       // real user, do stuff here
       next(null, {real: data});
    }
    

    and check for errors in the client js

      socket.emit( 'plugins.YourPlugin.formSubmit', $( this ).serializeArray(), function (err, data) {
        if (err) return;
    
        if (data) {
          // do stuff
        }
      });
    

    and if you need to check the user in the client for some reason, it is in the variable app.user.uid



  • I want to redirect user to login as when you try to go any other private page like /unread


  • Community Rep

    Sure, you do that where you declare your custom route.

    function renderSink(req, res, next) {
    	if (!req.uid) return res.redirect('/login');
    
    	// Do normal stuff.
    	res.render('demo', someData);
    }
    
    module.exports = function(app, middleware, controllers) {
    	app.get('/demo', middleware.buildHeader, renderSink);
    	app.get('/api/demo', renderSink);
    };
    

    More info here: http://expressjs.com/4x/api.html#res.redirect


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 4
  • 2
  • 4
| |