LÖVE CÖNNECTION (日本語)
LÖVE CÖNNECTION は Nicholas Scott (ニコラス・スコット) により開発および保守されているネットワーキング・モジュールです。
require() でモジュールが読み込まれたときに関数一覧テーブルが返されるため、 require で設定されたものは Net~ と等価であると想定します。
関数
· Net:connectedUsers() -- 現在接続されている利用者の一覧を返します。 · Net:kickUser( id, reason ) -- 指定された id により利用者を追い出してから現在の画面に理由を表示します。 · Net:isServer() -- サーバーが現在実行中ならば true を返します。 · Net:isClient() -- クライアントが現在実行中ならば true を返します。 · Net:setMaxPing( ping ) -- クライアントに呼び出された場合は再びサーバーへ Ping を送信する前に Ping 時間の設定を行い、サーバー実行中に呼び出された場合はクライアントに対してタイムアウトになる前に最大 Ping (ミリ秒) 値を設定します。 · Net:init( state ) -- Starts the networking, state needs to be either server, to run as a server, or client, to run as a client · Net:update( dt ) -- Add this into any ACTIVE love.update() and pass the deltatime from the love.update!! IMPORTANT!!!! · Net:connect( ip, port ) --If called as server you must set ip to nil and port to the port you want to listen on, defaults to 20024, when called as client ip and port are required, and port msut be the same as the servers, obviously :D · Net:disconnect() --Disconnects from online, if it is ran from client it will nil out the socket entirely and send a disconnect packet to the server it was on · Net:registerCMD( cmd, function ) --Registers a command for networking, cmd is a string name of the command you want to register, and function is the function ran when a client( or server ) receives the command specified. More on this below :D · Net:send( table, cmd, param, id ) -- Id is only supplied if running as server, table is a table of data you want to send, cmd is the command your running on the other end, must be registered, param is parameters to the command, id is the id of the client you want to send to( only if server leave nil if client) · Net:encode( table ) -- Encodes a table of data into a string to be sent across the network · Net:decode( string ) -- Decodes a string into a table of data when received
イベント These are called the same way you call love.update() function Net.event.server.userConnected( id ) end
クライアント・イベント
· Net.event.client.connect( ip, port) -- Called when client connects to a server with net:connect() ip is ip of server and port is port of server · Net.event.client.receive( table, dt, cmd, param ) -- Called when client receives a packet table is the table client sent, dt deltatime of net:update(), cmd is the command from client, param is parameters from the client · Net.event.client.disconnect() --Called when client disconnects from server with net:disconnect() · Net.event.client.cmdRegistered( cmd, functionS ) -- Called when a command is registered, cmd is the cmd and function is the function called by the cmd · Net.event.client.send( table, cmd, param ) -- Called when a packet is sent to the server, table is the table being sent, cmd is the cmd, param is parameters · Net.event.client.kickedFromServer( reason ) -- Called when we( the client ) is kicked from the server, reason is the reason for the kick
サーバー・イベント
· Net.event.server.userConnected( id ) -- 利用者がサーバーへ接続する時に呼び出されます。 · Net.event.server.userDisconnected( id ) -- 利用者がサーバーから切断された時に呼び出されます。 · Net.event.server.userTimedOut( id ) -- 利用者がサーバーからタイムアウトになった時に呼び出されます。 · Net.event.server.userKicked( id, reason ) --Called when a user is kicked, reason is the reason they were kicked · Net.event.server.receive( table, dt, id, cmd, param ) --Called when we receive a packet, table is the table sent by client, id is id of client, dt is deltatime of net:update(), cmd is the command sent by client, param is parameters of the client · Net.event.server.connect( port ) --Called when we connect to a port, port is the port connected to · Net.event.server.disconnect() --Called when we disconnect from the port and stop listening to packets · Net.event.server.cmdRegistered( cmd, functionS) --Called when we register a command, cmd is the command registered, function is the function called by the cmd · Net.event.server.send( table, cmd, param, id ) --Called when we send a packet to a client, table is the table sent, cmd is the cmd sent, param is the parameters sent, id is the id of the client we sent to
変数・テーブル
· Net.client = table --Holds Ip and port of currently connected server, if not connected ip and port are nil · Net.server = table --Holds the port currently being listened on, nil if not listening · Net.commands = table --Holds all currently registered commands in format CMD = FUNCTION, command editing function coming soon! · Net.users = table --Holds all currently connected users in format ID = PLAYERTABLE, player table editing function coming soon! · Net.maxPing = integer -- サーバー、あるいはクライアントにおける現在の最大 Ping 値です。 · Net.connected = boolean --True if connected to a server or listening to a port, false if not
チュートリアル
サーバーの設定
その 1: モジュールを変数として定義します。
Net = require( "net" )
その 2: ネットワークを初期化します。
Net:init( "Server" )
その 3: Connect to a port to listen for packets on
Net:connect( nil, 25045 )
その 4: Register some commands for received packets, the table is a table encoded and sent to us by the client :D useful for this
Net:registerCMD( "updateStatsOfPlayer", function( table, parameters, id, dt ) Net.users[id].name = table.name Net.users[id].color = table.color end )
その 5: Send some packets to a client! :D This will print to every client on the server's console "Hello There Mr. Client!" for id,playerdata in pairs( Net.users ) do Net:send( {}, "print", "Hello There Mr. Client!", id ) end
クライアントの設定
その 1: モジュールを変数として定義します。
Net = require( "net" )
その 2: ネットワークを初期化します。
Net:init( "Client" )
その 3: Connect to a server, this will also initialize the handshake feature and allow us to use net:send()
Net:connect( "127.0.0.1", 25045 )
その 4: Register some commands for received packets, the table is a table encoded and sent to us by the client :D useful for this
Net:registerCMD( "updateStatsOfBattlefield", function( table, parameters, id, dt )Tank.x = table.x Tank.y = table.y Tank.name = table.name end )
その 5: Send some packets to the server! :D This will print to the servers console "Hello there Mr. Server!", the {} is an empty table, THIS IS REQUIRE DON'T LEAVE IT NIL
Net:send( {}, "print", "Hello There Mr. Server!" )