Giter Site home page Giter Site logo

cod4x_plugin_http's Introduction

Http GSC Plugin

This plugin allows you to do http requests within your gsc scripts.

Usage:

callback(content) // callback function receiving the request content as string
{
    iprintln("out:" + content); // print the result
}

...

httpget("127.0.0.1/test.php", ::callback);

httpget interprets the received contents as a string which can be read from the callback

Compiling:

This plugin is written in D. You can get the needed compiler here and the dub build system here. To compile the plugin run dub --arch=x86 --build=release in the folder containing package.json.

Examples

Simply JSON HTTP GET Request

init()
{
	for(;;)
	{
		httpgetjson("http://freegeoip.net/json/8.8.8.8", ::callback); // get country for 8.8.8.8 every 5 seconds
		wait 5; 
	}	
}

callback(handle)
{
	if(handle == 0)
	{
		//error parsing json. Do something about this error case
		return;
	}
	res = jsongetstring(handle, "country_name");
	jsonreleaseobject(handle); // release the plugin internal json data
	iprintln(res); // prints country name
}                                                                 

Get deeply nested data in a JSON HTTP GET Request

test.php:

{                                                                                         
    "success": {                                                                          
        "total": 1                                                                        
    },                                                                                    
    "contents": {                                                                         
        "quotes": [                                                                       
            {                                                                             
                "quote": "If you cannot do great things, do small things in a great way.",
                "length": "62",                                                           
                "author": "Napoleon Hill",                                                
                "tags": [                                                                 
                    "inspire",                                                            
                    "small-things"                                                        
                ],                                                                        
                "category": "inspire",                                                    
                "date": "2016-09-05",                                                     
                "title": "Inspiring Quote of the day",                                    
                "background": "https://theysaidso.com/img/bgs/man_on_the_mountain.jpg",   
                "id": "VO7q_Ezldx9gvzlO4jujJgeF"                                          
            }                                                                             
        ]                                                                                 
    }                                                                                     
}    

_somescript.gsc:


init()
{
	for(;;)
	{
		httpgetjson("http://127.0.0.1/test.php", ::callback);
		wait 5;
	}

	level thread onPlayerConnect();	
}

callback(handle)
{
	if(handle == 0)
	{
		//error parsing json. Do something about this error case
		return;
	}
	jsongetint(handle, "contents.quotes.size"); //returns count of array elements. In this case it is 1.
	jsonsetstring(handle, "contents.quotes.0.magic", "val"); // sets magic to "val"
	jsongetstring(handle, "contents.quotes.0.magic"); // returns "val"
	jsongetstring(handle, "contents.quotes.0.quote"); // returns "If you cannot do great things, do small things in a great way."
	jsonreleaseobject(handle);
}

JSON HTTP POST Request

Authentication example

GSC:

onPlayerConnect()
{
	for(;;)
	{
		level waittill ( "connecting", player );
		
		sid = player getsteamid64();
		pid = player getplayerid64();
		name = player.name;
		httppostjson("http://test.local/playerjoins", 
					 "{'steamid64':" + sid + ", 'playerid64':" + pid +", 'name':'" + name + "'}", // JSON encoded post data
					 ::playerjoinsCallback,
					 player); // if a entity is provided "self" is set in the callback 
	
		player thread onPlayerSpawned();
	}
}

playerjoinsCallback(handle)
{
	self iprintln("joined"); // self is set to the connected player
	jsonreleaseobject(handle);
}

cod4x_plugin_http's People

Contributors

d4edalus avatar danburg avatar iceninjaman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cod4x_plugin_http's Issues

Error: unknown function: httppostjson

Error:
******* script compile error *******
Error: unknown function: (file 'pr/_logger.gsx', line 231)
httppostjson( hurl, json, ::httpLogCallback );

Plugin is loaded and "httpgetjson" function is working, this is from log, notice there is no command "httppostjson".

Adding a plugin script function for plugin 0, command name: httpGet.
Adding a plugin script function for plugin 0, command name: httpGetJson.
Adding a plugin script function for plugin 0, command name: jsonGetInt.
Adding a plugin script function for plugin 0, command name: jsonSetInt.
Adding a plugin script function for plugin 0, command name: jsonGetString.
Adding a plugin script function for plugin 0, command name: jsonSetString.
Adding a plugin script function for plugin 0, command name: jsonReleaseObject.
Plugin libcod4x_http_plugin loaded successfully. Server is currently running 1 plugins.

Thx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.