• Stars
    star
    590
  • Rank 75,377 (Top 2 %)
  • Language
    C#
  • License
    GNU General Publi...
  • Created over 12 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A TcpClient-based HTTP library for Unity.

Attribution

Based on Simon Wittber's UnityWeb code (http://code.google.com/p/unityweb/).

LICENSE

UnityHTTP falls under the GPL due to its basis on Simon Wittber's UnityWeb code, which is licensed under the GPL.

You should be aware of this license and determine if it is acceptable for your project.

About

This is a TcpClient-based HTTP library for use in Unity. It should work in both the standalone player and in the web player.

It also has convenience methods for working with JSON.

Examples

IEnumerator example:

public IEnumerator SomeRoutine() {
    HTTP.Request someRequest = new HTTP.Request( "get", "http://someurl.com/somewhere" );
    someRequest.Send();

    while( !someRequest.isDone )
    {
        yield return null;
    }

    // parse some JSON, for example:
    JSONObject thing = new JSONObject( request.response.Text );
}

Closure-style (does not need to be in a coroutine):

HTTP.Request someRequest = new HTTP.Request( "get", "http://someurl.com/somewhere" );
someRequest.Send( ( request ) => {
    // parse some JSON, for example:
    JSONObject thing = new JSONObject( request.response.Text );
});

Post request using form data:

WWWForm form = new WWWForm();
form.AddField( "something", "yo" );
form.AddField( "otherthing", "hey" );

HTTP.Request someRequest = new HTTP.Request( "post", "http://someurl.com/some/post/handler", form );
someRequest.Send( ( request ) => {
    // parse some JSON, for example:
    bool result = false;
    Hashtable thing = (Hashtable)JSON.JsonDecode( request.response.Text, ref result );
    if ( !result )
    {
        Debug.LogWarning( "Could not parse JSON response!" );
        return;
    }
});

Post request using JSON:

Hashtable data = new Hashtable();
data.Add( "something", "hey!" );
data.Add( "otherthing", "YO!!!!" );

// When you pass a Hashtable as the third argument, we assume you want it send as JSON-encoded
// data.  We'll encode it to JSON for you and set the Content-Type header to application/json
HTTP.Request theRequest = new HTTP.Request( "post", "http://someurl.com/a/json/post/handler", data );
theRequest.Send( ( request ) => {

    // we provide Object and Array convenience methods that attempt to parse the response as JSON
    // if the response cannot be parsed, we will return null
    // note that if you want to send json that isn't either an object ({...}) or an array ([...])
    // that you should use JSON.JsonDecode directly on the response.Text, Object and Array are
    // only provided for convenience
    Hashtable result = request.response.Object;
    if ( result == null )
    {
        Debug.LogWarning( "Could not parse JSON response!" );
        return;
    }
  
});

If you want to make a request while not in Play Mode (e. g. from a custom Editor menu command or wizard), you must use the Request synchronously, since Unity's main update loop is not running. The call will block until the response is available.

Hashtable data = new Hashtable();
data.Add( "something", "hey!" );
data.Add( "otherthing", "YO!!!!" );

HTTP.Request theRequest = new HTTP.Request("post", "http://someurl.com/a/json/post/handler", data );
theRequest.synchronous = true;
theRequest.Send((request) => {
	EditorUtility.DisplayDialog("Request was posted.", request.response.Text, "Ok");
});

More Repositories

1

TurnBased

A node.js engine to support turn-based games.
JavaScript
20
star
2

grmble

A node.js chat server/client application.
JavaScript
12
star
3

perlin

A node.js perlin noise generator library.
JavaScript
9
star
4

node-storehouse

A small, simple HTTP file upload server for node.js.
JavaScript
8
star
5

ksuid-cli

A CLI for dealing with KSUIDs
JavaScript
6
star
6

js-sizeof

A small, dependency-free library to calculate the rough size of a javascript object in memory.
JavaScript
5
star
7

simplecoder

Encode values using human-friendly dictionaries.
JavaScript
5
star
8

boxcutter

A utility knife for interacting with package.json (or other json files)
JavaScript
4
star
9

enveloper

enveloper is a small, simple utility to encrypt secrets
JavaScript
3
star
10

perlbot

An IRC bot written in perl.
Perl
3
star
11

LevelUp

JavaScript
2
star
12

bitflood

A prioritized p2p chunked file transfer protocol.
C++
2
star
13

node-devents

A simple Redis-based distributed EventEmitter.
JavaScript
1
star
14

node-geoip-server

Simple HTTP GeoIP API server.
JavaScript
1
star
15

fs-fastify

Drive your fastify setup from the filesystem.
JavaScript
1
star
16

webajob

A historical archive of a project circa 2003-4 to create a social jobs site.
Perl
1
star
17

node-maxlength-stream

A simple transform stream that limits the max length of the stream.
JavaScript
1
star
18

mongoose-simpletimestamps

Simple timestamps plugin for mongoose.
JavaScript
1
star
19

node-delver

Read and update nested objects using simple patterns.
JavaScript
1
star
20

ratecontrol

A simple rate controller for connect-style servers.
JavaScript
1
star
21

phoneparser

A parser for phone numbers.
JavaScript
1
star
22

lobax

A Dynamically Configurable Load Balancer in Node.js
JavaScript
1
star
23

easyid

A configurable, easy short id generator for humans.
JavaScript
1
star
24

RPC-Lite

A lightweight perl RPC implementation.
Perl
1
star