Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/selimanac/defold-tiny-http
Simple http server and client for Defold
https://github.com/selimanac/defold-tiny-http
defold defold-game-engine defold-library http http-client http-server server
Last synced: about 1 month ago
JSON representation
Simple http server and client for Defold
- Host: GitHub
- URL: https://github.com/selimanac/defold-tiny-http
- Owner: selimanac
- License: mit
- Created: 2019-10-23T14:49:19.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-10-29T08:39:10.000Z (about 5 years ago)
- Last Synced: 2024-11-02T14:33:25.687Z (about 2 months ago)
- Topics: defold, defold-game-engine, defold-library, http, http-client, http-server, server
- Language: C++
- Homepage: https://selimanac.github.io/
- Size: 3.74 MB
- Stars: 11
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-defold - Tiny Http
README
![Tiny Http Native Extension](https://selimanac.github.io/assets/gfx/tiny_http_dark.png)
Tiny Http - Defold Native Extension is a simple http server and client.
All requests and responses are JSON. You may consider of using [CJSON](https://github.com/Melsoft-Games/defold-cjson) for encoding and decoding.
All server responses for GET requests are static echo. POST responses are limited but customizable by using [`dhttp.server_post_content`](#dhttpserver_post_contentjson).
Server works on iOS, Android, MacOS, Win 10 and Linux(Debian 10.x). HTML5 build is not supported. Since I don't need it, SSL not supported. But it is [possible](https://github.com/yhirose/cpp-httplib#openssl-support) to add this feature, feel free to PR if you want to.
## Installation
You can use Tiny Http in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your game.project file and in the dependencies field under project add:https://github.com/selimanac/defold-tiny-http/archive/master.zip
---## Examples
Detailed server and client examples can be found in [examples](https://github.com/selimanac/defold-tiny-http/tree/master/examples) folder.
### Server
```lua
-- Server settings
local host = "localhost"
local port = 8800-- Server callbacks
local function server_callbacks(self, message)
-- Decode response
local jresult = json.decode(message.result)
local event_id = message.event_idif jresult.server_status == dhttp.SERVER_START then
-- Server started
elseif jresult.server_status == dhttp.SERVER_STOP then
-- Server stopped
else
if event_id == 123 then
pprint(jresult)
-- Do something fancy
end
end
endfunction init(self)
-- Start the server
dhttp.server_start(host, port, server_callbacks)
end
```### Client
```lua
-- Client settings
local host = "localhost"
local port = 8800-- Client callbacks
local function client_callbacks(self, message)
-- Decode response
local jresult = json.decode(message.result)-- Check error
if jresult.error then
print("Error: ", jresult.error)
return
endpprint(jresult)
endfunction init(self)
-- Init the client
dhttp.client_start(host, port, client_callbacks)-- Get endpoint
dhttp.client_get("/hi", 123)
end
```## Server API
#### dhttp.server_start(`host, port, callback, [log], [error], [endpoints]`)
Init and start the server.
| Param | Desc |
| ------------- | ------------- |
| `host` | (_string_) Host address or IP |
| `port` | (_int_) Port number |
| `callback` | (_function_) Callback function |
| `[log]` | (_boolean_) Turn logging on/off. Default is false |
| `[error]` | (_boolean_) Turn error responses on/off. Default is true |
| `[endpoints]` | (_table_) Optional endpoints |#### dhttp.server_stop()
Gracefully shutdown the server.
#### dhttp.is_server_running()
Check if server is running. Returns boolean.
#### dhttp.server_post_content(`content`)
Sets the POST response for all POST endpoints. You can set this anytime.
| Param | Desc |
| ------------- | ------------- |
| `content ` | (string) JSON formated string |### Endpoints
There are several built-in endpoints.
| Endpoint | Desc |
| ------------- | ------------- |
| "/hi" | Says hi! |
| "/num/(\d+)" | (_GET_) Gets only [0-9] as integer |
| "/str/(\w+)" | (_GET_) Gets only [a-zA-Z0-9] as string |
| "/post" | (_POST_) Generic post endpoint with params |
| "/stop" | (_GET_) Stop the server |You can define custom endpoints.
Endpoints support regex. But "?" character is reserved and may cause a crash.```lua
local endpoints = {
{
endpoint_type = dhttp.METHOD_GET,
endpoint = "/monster/(\\d+)"
},
{
endpoint_type = dhttp.METHOD_POST,
endpoint = "/move"
}
}dhttp.server_start("localhost", 8888, server_callbacks, false, true, endpoints)
```
## Client API
#### dhttp.client_start(`host, port, client_callbacks`)
Init the client.
| Param | Desc |
| ------------- | ------------- |
| `host` | (_string_) Host address or IP |
| `port` | (_int_) Port number |
| `callback` | (_function_) Callback function |#### dhttp.client_hi()
Defold says hi!
#### dhttp.client_get(`endpoint, [event_id]`)
| Param | Desc |
| ------------- | ------------- |
| `endpoint` | (_string_) Endpoint address |
| `[event_id]`| (_int_) Event ID for tracking the action |Event IDs are for tracking the requests on server and client. They send as a header. You can easily group and parse your triggers by using event ids.
#### dhttp.client_post(`endpoint, params, [event_id]`)
| Param | Desc |
| ------------- | ------------- |
| `endpoint` | (_string_) Endpoint address |
| `params` | (_string_) JSON formated string |
| `[event_id]`| (_int_) Event ID for tracking the action |Event IDs are for tracking the requests on server and client. They send as a header. You can easily group and parse your triggers by using event ids.
```lua
local temp_table = {
x = 10,
y = 20
}jresult = cjson.encode(temp_table)
local params = {
position = jresult
}dhttp.client_post("/post", params, 1)
```#### Constants
##### dhttp.METHOD_GET
##### dhttp.METHOD_POST
##### dhttp.SERVER_START
##### dhttp.SERVER_STOP## Dependencies
Build by using [cpp-httplib](https://github.com/yhirose/cpp-httplib)
Characters by [@bevouliin](https://twitter.com/bevouliin)