Ver código fonte

Added first version of a networking manual

Björn Ritzl 6 anos atrás
pai
commit
ee439003c8
2 arquivos alterados com 59 adições e 0 exclusões
  1. 4 0
      docs/en/en.json
  2. 55 0
      docs/en/manuals/networking.md

+ 4 - 0
docs/en/en.json

@@ -386,6 +386,10 @@
                     {
                         "path": "/manuals/modules",
                         "name": "Modules"
+                    },
+                    {
+                        "path": "/manuals/networking",
+                        "name": "Networking"
                     }
                 ]
             },

+ 55 - 0
docs/en/manuals/networking.md

@@ -0,0 +1,55 @@
+---
+title: Networking in Defold
+brief: This manual explains how to connect to remote servers and perform other kinds of network connections.
+---
+
+# Networking
+
+It is not uncommon for games to have some kind of connection to a backend service, perhaps to post scores, handle match making or store saved games in the cloud. Many games also have peer to peer connections where game clients communicate directly with each other, without involvement of a central server.
+
+
+## HTTP requests
+
+Defold can make normal HTTP requests using the `http.request()` function. Example:
+
+```Lua
+local function handle_response(self, id, response)
+	print(response.status, response.response)
+end
+
+http.request("https://www.defold.com", "GET", handle_response)
+```
+
+This will make an HTTP GET request to https://www.defold.com. The function is asynchronous and will not block while making the request. Once the request has been made and a server has sent a response it will invoke/call the provided callback function. The callback function will receive the full server response, including status code and response headers. It is also possible to make HTTP POST requests to pass data to the server and to specify request headers. Refer to the [API reference](/ref/http/) to learn more.
+
+Using HTTP requests allows you to connect to and interact with thousands of different services on the internet. In most cases there's more to it than simply making an HTTP request. You usually need to use some kind of authenticate, the request data may need to be formatted in a certain way and the response may need to be parsed before it can be used. This can of course be done manually by you but there are also extensions and libraries to take care of this sort of thing for you. Below you'll find a list of some extensions that can be used to more easily interact with specific backend services:
+
+* [AWS SDK](https://github.com/britzl/aws-sdk-lua) - Use Amazon Web Services from within your game
+* [Colyseus](https://github.com/colyseus/colyseus-defold) - Multiplayer game client
+* [Firebase Analytics](https://github.com/defold/extension-firebase-analytics) - Add Firebase Analytics to your game
+* [Google Analytics](https://github.com/britzl/defold-googleanalytics) - Add Google Analytics to your game
+* [Google Play Game Services](https://github.com/defold/extension-gpgs) - Use Google Play Game Services to authenticate and use cloud save in your game
+* [PlayFab](https://github.com/PlayFab/LuaSdk) - Add authentication, matchmaking, analytics, cloud save and more to your game
+* [Steamworks](https://github.com/britzl/steamworks-defold/) - Add Steam support to your game
+
+Check the [Asset Portal](https://www.defold.com/community/assets/) for even more extensions!
+
+
+## Socket connections
+
+Defold includes the [LuaSocket library](http://w3.impa.br/~diego/software/luasocket/) for creating TCP and UDP socket connections. Creating a socket connection, sending some data and reading a response is a little bit more involved:
+
+```Lua
+local client = socket.tcp()
+client:connect("127.0.0.1", 8123)
+client:settimeout(0) -- non blocking socket
+client:send("foobar") -- send data like this
+local response = client:receive("*l") -- receive a "line" of data like this
+```
+
+This will create a TCP socket, connect it to IP 127.0.0.1 (localhost) and port 8123. It will set timeout to 0 to make the socket non-blocking and it will send the string "foobar" over the socket. It will also read a line of data (bytes ending with a newline character) from the socket. Note that the above example doesn't contain any kind of error handling. Refer to the [API reference](/ref/socket/) to learn more about the functionality available via LuaSocket. The [official LuaSocket documentation](http://w3.impa.br/~diego/software/luasocket/) also contains many examples of how to work with the library. There is also some examples and helper modules in the [DefNet library](https://github.com/britzl/defnet/).
+
+
+## WebSocket connections
+
+Defold doesn't contain any out of the box solution for creating WebSocket connections. For WebSocket connectivity it is recommended to use the [Defold-WebSocket extension](https://github.com/britzl/defold-websocket).