Difference between revisions of "LÖVE CÖNNECTION (日本語)"

(Created page with "LÖVE CÖNNECTION は [http://steamcommunity.com/id/NicholasScott Nicholas Scott] (ニコラス・スコット) により開発および保守されているネットワーキ...")
 
m
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
LÖVE CÖNNECTION は [http://steamcommunity.com/id/NicholasScott Nicholas Scott] (ニコラス・スコット) により開発および保守されているネットワーキング・モジュールです。
+
LÖVE CÖNNECTION は [https://love2d.org/forums/viewtopic.php?t=80330 Nicholas Scott] (ニコラス・スコット) により開発および保守されているネットワーキング・モジュールです。
  
 
require() でモジュールが読み込まれたときに関数一覧テーブルが返されるため、 require で設定されたものは Net~ と等価であると想定します。
 
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 ) ''-- ネットワークを開始します。サーバとして、またはクライアントとしての実行、クライアントからの実行のいずれかのサーバ状態が必要です。''
 +
* Net:update( dt )'' -- 全ての '''ACTIVE''' を love.update()  へ追加して、love.update からデルタタイムを引き渡してください!! 重要!!!''
 +
* Net:connect( ip, port )'' -- サーバから呼び出だされる場合は、 IP へ nil を、そして port へ接続待機を行うポート番号を設定する必要があります。デフォルトのポート番号は 20024 です。クライアントから呼び出だされる場合は IPアドレスおよびポートが必要です。そして、明らかなことですがポートはサーバと同じにする必要があります。 :D''
 +
* Net:disconnect()'' -- クライアントからソケットの出力がすべて nil となったかサーバーに切断パケットが送信された場合は、オンラインから切断します。''
 +
* Net:registerCMD( cmd, function )'' -- ネットワークに対してコマンドを登録します。 cmd は登録したい文字列形式のコマンド名です。 function は指定されたコマンドを受信したときにクライアント(またはサーバ)で実行される関数です。詳細は後述します :D''
 +
* Net:send( table, cmd, param, id )'' -- id はサーバ実行中にのみ指定します。 table は送信したいデータの格納されているテーブルです。 cmd はコマンドであり通信先で実行されますが、登録されている必要があります。 param はコマンドの引数です。 id は送信をしたいクライアントの ID です (クライアントの場合はサーバーを nil にする場合に限ります)。''
 +
* Net:encode( table )'' -- ネットワークを経由して送信する文字列データを有するテーブルをエンコードします。''
 +
* Net:decode( string )'' -- 受信時に文字列データのテーブルをデコードします。''
  
    · Net:connectedUsers()'' -- 現在接続されている利用者の一覧を返します。''
+
== イベント ==
    · Net:kickUser( id, reason )'' -- 指定された id により利用者を追い出してから現在の画面に理由を表示します。''
+
''これらは <code>love.update ()</code> 関数を呼び出すのと同じ方法で呼び出されます。つまり、 <code>Net.event.server.userConnected( id ) end</code> です。''
    · 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)'' -- net:connect() によりクライアントからサーバへ接続されたときに呼び出されます。 IP はサーバの IP アドレス、および port はサーバのポートです。''
 +
* Net.event.client.receive( table, dt, cmd, param )'' -- クライアントがパケットを受信したときに呼び出されます。 table はクライアントから送信されたテーブルです。 id はクライアントの ID です。 dt は net:update() のデルタタイムです。 cmd はクライアントから送信されたコマンドです。 param はクライアントの引数です。''
 +
* Net.event.client.disconnect()'' -- net:disconnect() によりサーバから切断されたときに呼び出されます。''
 +
* Net.event.client.cmdRegistered( cmd, functionS )'' -- コマンド登録されたときに呼び出されます。 cmd は登録されたコマンドであり、 function は cmd により呼び出だされる関数です。''
 +
* Net.event.client.send( table, cmd, param )'' -- サーバへパケットを送信するときに呼び出されます。 table は送信するテーブルです。 cmd はコマンドです。 param は引数です。''
 +
* Net.event.client.kickedFromServer( reason )'' -- 利用者がサーバーから強制的に退場させられた時に呼び出されます(クライアント)。 reason は利用者が退場させられた理由です。''
  
クライアント・イベント
 
  
    · 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 )'' -- 利用者が強制的に退場させられた時に呼び出されます。 reason は利用者が退場させられた理由です。''
 +
* Net.event.server.receive( table, dt, id, cmd, param )'' -- パケットを受信したときに呼び出されます。 table はクライアントから送信されたテーブルです。 id はクライアントの ID です。 dt は net:update() のデルタタイムです。 cmd はクライアントから送信されたコマンドです。 param はクライアントの引数です。''
 +
* Net.event.server.connect( port )'' -- ポートへ接続されたときに呼び出されます。 port は接続先のポートです。''
 +
* Net.event.server.disconnect()'' -- ポートからの切断およびパケットの受信待機停止状態になったときに呼び出されます。''
 +
* Net.event.server.cmdRegistered( cmd, functionS)'' -- コマンド登録されたときに呼び出されます。 cmd は登録されたコマンドであり、 function は cmd により呼び出だされる関数です。''
 +
* Net.event.server.send( table, cmd, param, id )'' -- クライアントへパケットを送信するときに呼び出されます。 table は送信するテーブルです。 cmd は送信するコマンドです。 param は送信する引数です。 id はクライアントが送信する ID です。''
  
    · Net.event.server.userConnected( id )'' -- 利用者がサーバーへ接続する時に呼び出されます。''
+
== 変数・テーブル ==
    · Net.event.server.userDisconnected( id )'' -- 利用者がサーバーから切断された時に呼び出されます。''
+
* Net.client = table'' -- 現在接続されているサーバの IPアドレスとポートを保持しており、未接続の場合は IPアドレスとポートは nil となります。''
    · Net.event.server.userTimedOut( id )'' -- 利用者がサーバーからタイムアウトになった時に呼び出されます。''
+
* Net.server = table'' -- ポートが現在接続待機状態であるかを保持しており、接続待機状態でなければ nil となります。''
    · Net.event.server.userKicked( id, reason )'' --Called when a user is kicked, reason is the reason they were kicked''
+
* Net.commands = table'' -- コマンド = 関数の形式で現在登録されている全てのコマンドを保持しています。コマンド編集用の関数は近いうちに公開予定です!''
    · 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.users = table'' -- ID = PLAYERTABLE の形式で現在登録されている全ての遊戯者を保持しています。遊戯者テーブル編集用の関数は近いうちに公開予定です!''
    · Net.event.server.connect( port )'' --Called when we connect to a port, port is the port connected to''
+
* Net.maxPing = integer'' -- サーバー、あるいはクライアントにおける現在の最大 Ping 値です。''
    · Net.event.server.disconnect()'' --Called when we disconnect from the port and stop listening to packets''
+
* Net.connected = boolean'' -- サーバへ接続しているかポートが接続待機状態ならば true であり、それ以外は false です。''
    · 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''
+
その 1: モジュールを変数として定義します。
    · Net.commands = table'' --Holds all currently registered commands in format CMD = FUNCTION, command editing function coming soon!''
+
<source lang="lua">
    · Net.users = table'' --Holds all currently connected users in format ID = PLAYERTABLE, player table editing function coming soon!''
+
Net = require( "net" )
    · Net.maxPing = integer'' -- サーバー、あるいはクライアントにおける現在の最大 Ping 値です。''
+
</source>
    · Net.connected = boolean'' --True if connected to a server or listening to a port, false if not''
+
 
 +
その 2: ネットワークを初期化します。
 +
<source lang="lua">
 +
Net:init( "Server" )
 +
</source>
 +
 
 +
その 3: パケットの受信待機をするためにポートへ接続します。
 +
<source lang="lua">
 +
Net:connect( nil, 25045 )
 +
</source>
 +
 
 +
その 4: 受信されたパケットに対してあるコマンドの登録を行います。 table は利用者によりエンコードされ、送信されたテーブルです。 :D このようにすると便利です
 +
<source lang="lua">
 +
Net:registerCMD( "updateStatsOfPlayer", function( table, parameters, id, dt ) Net.users[id].name = table.name Net.users[id].color = table.color end )
 +
</source>
  
'''チュートリアル'''
+
その 5: クライアントへあるパケットを送信します! :D これはサーバのコンソールから各クライアントへ "Hello There Mr. Client!" を表示します。
 +
<source lang="lua">
 +
for id,playerdata in pairs( Net.users ) do
 +
    Net:send( {}, "print", "Hello There Mr. Client!", id )
 +
end
 +
</source>
  
''サーバーの設定''
 
  
 +
=== クライアントの設定 ===
 
その 1: モジュールを変数として定義します。
 
その 1: モジュールを変数として定義します。
Net = require( "net" )  
+
<source lang="lua">
 +
Net = require( "net" )  
 +
</source>
 +
 
 
その 2: ネットワークを初期化します。
 
その 2: ネットワークを初期化します。
Net:init( "Server" )
+
<source lang="lua">
その 3: Connect to a port to listen for packets on
+
Net:init( "Client" )  
Net:connect( nil, 25045 )
+
</source>
その 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
 
  
''クライアントの設定''
+
その 3: サーバへの接続を行います。これは接続機能の初期化も行い net:send() を使用可能にします。
 +
<source lang="lua">
 +
Net:connect( "127.0.0.1", 25045 )
 +
</source>
  
その 1: モジュールを変数として定義します。
+
その 4: 受信されたパケットに対してあるコマンドの登録を行います。 table は利用者によりエンコードされ、送信されたテーブルです。 :D このようにすると便利です
Net = require( "net" )
+
<source lang="lua">
その 2: ネットワークを初期化します。
+
Net:registerCMD( "updateStatsOfBattlefield", function( table, parameters, id, dt )Tank.x = table.x Tank.y = table.y Tank.name = table.name end )  
Net:init( "Client" )
+
</source>
その 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 )
+
その 5: サーバへあるパケットを送信します! :D これはサーバのコンソールへ "Hello There Mr. Server!" を表示します。 <code>{}</code> は空のテーブルですが、 NIL は必要なものであり除去してはいけません。
その 4: Register some commands for received packets, the table is a table encoded and sent to us by the client :D useful for this
+
<source lang="lua">
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!" )
 
  Net:send( {}, "print", "Hello There Mr. Server!" )
 +
</source>
  
 
[[Category:Libraries (日本語)]]
 
[[Category:Libraries (日本語)]]
 +
 +
== そのほかの言語 ==
 +
{{i18n (日本語)|LÖVE_CÖNNECTION}}

Latest revision as of 03:16, 16 December 2019

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 ) -- ネットワークを開始します。サーバとして、またはクライアントとしての実行、クライアントからの実行のいずれかのサーバ状態が必要です。
  • Net:update( dt ) -- 全ての ACTIVE を love.update() へ追加して、love.update からデルタタイムを引き渡してください!! 重要!!!
  • Net:connect( ip, port ) -- サーバから呼び出だされる場合は、 IP へ nil を、そして port へ接続待機を行うポート番号を設定する必要があります。デフォルトのポート番号は 20024 です。クライアントから呼び出だされる場合は IPアドレスおよびポートが必要です。そして、明らかなことですがポートはサーバと同じにする必要があります。 :D
  • Net:disconnect() -- クライアントからソケットの出力がすべて nil となったかサーバーに切断パケットが送信された場合は、オンラインから切断します。
  • Net:registerCMD( cmd, function ) -- ネットワークに対してコマンドを登録します。 cmd は登録したい文字列形式のコマンド名です。 function は指定されたコマンドを受信したときにクライアント(またはサーバ)で実行される関数です。詳細は後述します :D
  • Net:send( table, cmd, param, id ) -- id はサーバ実行中にのみ指定します。 table は送信したいデータの格納されているテーブルです。 cmd はコマンドであり通信先で実行されますが、登録されている必要があります。 param はコマンドの引数です。 id は送信をしたいクライアントの ID です (クライアントの場合はサーバーを nil にする場合に限ります)。
  • Net:encode( table ) -- ネットワークを経由して送信する文字列データを有するテーブルをエンコードします。
  • Net:decode( string ) -- 受信時に文字列データのテーブルをデコードします。

イベント

これらは love.update () 関数を呼び出すのと同じ方法で呼び出されます。つまり、 Net.event.server.userConnected( id ) ~ end です。

クライアント・イベント

  • Net.event.client.connect( ip, port) -- net:connect() によりクライアントからサーバへ接続されたときに呼び出されます。 IP はサーバの IP アドレス、および port はサーバのポートです。
  • Net.event.client.receive( table, dt, cmd, param ) -- クライアントがパケットを受信したときに呼び出されます。 table はクライアントから送信されたテーブルです。 id はクライアントの ID です。 dt は net:update() のデルタタイムです。 cmd はクライアントから送信されたコマンドです。 param はクライアントの引数です。
  • Net.event.client.disconnect() -- net:disconnect() によりサーバから切断されたときに呼び出されます。
  • Net.event.client.cmdRegistered( cmd, functionS ) -- コマンド登録されたときに呼び出されます。 cmd は登録されたコマンドであり、 function は cmd により呼び出だされる関数です。
  • Net.event.client.send( table, cmd, param ) -- サーバへパケットを送信するときに呼び出されます。 table は送信するテーブルです。 cmd はコマンドです。 param は引数です。
  • Net.event.client.kickedFromServer( reason ) -- 利用者がサーバーから強制的に退場させられた時に呼び出されます(クライアント)。 reason は利用者が退場させられた理由です。


サーバー・イベント

  • Net.event.server.userConnected( id ) -- 利用者がサーバーへ接続する時に呼び出されます。
  • Net.event.server.userDisconnected( id ) -- 利用者がサーバーから切断された時に呼び出されます。
  • Net.event.server.userTimedOut( id ) -- 利用者がサーバーからタイムアウトになった時に呼び出されます。
  • Net.event.server.userKicked( id, reason ) -- 利用者が強制的に退場させられた時に呼び出されます。 reason は利用者が退場させられた理由です。
  • Net.event.server.receive( table, dt, id, cmd, param ) -- パケットを受信したときに呼び出されます。 table はクライアントから送信されたテーブルです。 id はクライアントの ID です。 dt は net:update() のデルタタイムです。 cmd はクライアントから送信されたコマンドです。 param はクライアントの引数です。
  • Net.event.server.connect( port ) -- ポートへ接続されたときに呼び出されます。 port は接続先のポートです。
  • Net.event.server.disconnect() -- ポートからの切断およびパケットの受信待機停止状態になったときに呼び出されます。
  • Net.event.server.cmdRegistered( cmd, functionS) -- コマンド登録されたときに呼び出されます。 cmd は登録されたコマンドであり、 function は cmd により呼び出だされる関数です。
  • Net.event.server.send( table, cmd, param, id ) -- クライアントへパケットを送信するときに呼び出されます。 table は送信するテーブルです。 cmd は送信するコマンドです。 param は送信する引数です。 id はクライアントが送信する ID です。

変数・テーブル

  • Net.client = table -- 現在接続されているサーバの IPアドレスとポートを保持しており、未接続の場合は IPアドレスとポートは nil となります。
  • Net.server = table -- ポートが現在接続待機状態であるかを保持しており、接続待機状態でなければ nil となります。
  • Net.commands = table -- コマンド = 関数の形式で現在登録されている全てのコマンドを保持しています。コマンド編集用の関数は近いうちに公開予定です!
  • Net.users = table -- ID = PLAYERTABLE の形式で現在登録されている全ての遊戯者を保持しています。遊戯者テーブル編集用の関数は近いうちに公開予定です!
  • Net.maxPing = integer -- サーバー、あるいはクライアントにおける現在の最大 Ping 値です。
  • Net.connected = boolean -- サーバへ接続しているかポートが接続待機状態ならば true であり、それ以外は false です。

チュートリアル

サーバーの設定

その 1: モジュールを変数として定義します。

Net = require( "net" )

その 2: ネットワークを初期化します。

Net:init( "Server" )

その 3: パケットの受信待機をするためにポートへ接続します。

Net:connect( nil, 25045 )

その 4: 受信されたパケットに対してあるコマンドの登録を行います。 table は利用者によりエンコードされ、送信されたテーブルです。 :D このようにすると便利です

Net:registerCMD( "updateStatsOfPlayer", function( table, parameters, id, dt ) Net.users[id].name = table.name Net.users[id].color = table.color end )

その 5: クライアントへあるパケットを送信します! :D これはサーバのコンソールから各クライアントへ "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: サーバへの接続を行います。これは接続機能の初期化も行い net:send() を使用可能にします。

Net:connect( "127.0.0.1", 25045 )

その 4: 受信されたパケットに対してあるコマンドの登録を行います。 table は利用者によりエンコードされ、送信されたテーブルです。 :D このようにすると便利です

Net:registerCMD( "updateStatsOfBattlefield", function( table, parameters, id, dt )Tank.x = table.x Tank.y = table.y Tank.name = table.name end )

その 5: サーバへあるパケットを送信します! :D これはサーバのコンソールへ "Hello There Mr. Server!" を表示します。 {} は空のテーブルですが、 NIL は必要なものであり除去してはいけません。

 Net:send( {}, "print", "Hello There Mr. Server!" )

そのほかの言語