Modules/Sockets/onSockOpened: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<pageclass class="#AA7592" subcaption="Sockets Module"></pageclass>
__NOTOC__
__NOTOC__
{{ModuleFunction|Sockets}}
{{ModuleFunction|Sockets}}
Line 4: Line 5:


===Parameters===
===Parameters===
* '''socket''' userdata representing a socket.
<syntaxhighlight lang="lua">
userdata socket
</syntaxhighlight>
 
* '''socket''': userdata representing a socket.


===Source===
===Source===
The source of this event is getRootElement()
The [[event system#Event source|source]] of this event is the [[root element]].


==Example==
==Example==
Line 18: Line 23:
   function (socket)
   function (socket)
       if socket == ircSocket then
       if socket == ircSocket then
         sockWrite(socket,"USER mta mta * MCvarial & Gamesnert")
         sockWrite(socket,"USER mta mta * :MCvarial & Gamesnert\r\n")
         sockWrite(socket,"NICK mta")
         sockWrite(socket,"NICK mta\r\n")
         sockWrite(socket,"JOIN #mta")
         sockWrite(socket,"JOIN #mta\r\n")


         outputServerLog("IRC: Connected!")
         outputServerLog("IRC: Connected!")
Line 51: Line 56:
==See Also==
==See Also==
===Functions===
===Functions===
* [http://wiki.mtasa.com/wiki/Modules/Sockets/sockOpen sockOpen()]
{{Modules/Sockets/Functions}}
* [http://wiki.mtasa.com/wiki/Modules/Sockets/sockWrite sockWrite()]
* [http://wiki.mtasa.com/wiki/Modules/Sockets/sockClose sockClose()]


===Events===
===Events===
* [http://wiki.mtasa.com/wiki/Modules/Sockets/onSockOpened onSockOpened]
{{Modules/Sockets/Events}}
* [http://wiki.mtasa.com/wiki/Modules/Sockets/onSockData onSockData]
* [http://wiki.mtasa.com/wiki/Modules/Sockets/onSockClosed onSockClosed]
[[Category:Modules]]

Latest revision as of 17:27, 12 May 2010


Package-x-generic.png This function is provided by the external module Sockets. You must install this module to use this function.

This event is triggered when a socket was created.

Parameters

userdata socket
  • socket: userdata representing a socket.

Source

The source of this event is the root element.

Example

This piece of code connects to irc.gtanet.com, joins #mta and quits in 10 seconds.

local root = getRootElement()
local ircSocket = sockOpen("irc.gtanet.com",6667)

addEventHandler("onSockOpened",root,
   function (socket)
      if socket == ircSocket then
         sockWrite(socket,"USER mta mta * :MCvarial & Gamesnert\r\n")
         sockWrite(socket,"NICK mta\r\n")
         sockWrite(socket,"JOIN #mta\r\n")

         outputServerLog("IRC: Connected!")
         setTimer(disconnect,10000,1)
      end
   end
)

addEventHandler("onSockData",root,
   function (socket,data)
      if socket == ircSocket then
         outputServerLog(data)
      end
   end
)

addEventHandler("onSockClosed",root,
   function (socket)
      if socket == ircSocket then
         outputServerLog("IRC: disconnected!")
      end
   end
)

function disconnect ()
   sockClose(ircSocket)
end

See Also

Functions

Events