Modules/Sockets/sockOpen: Difference between revisions
Jump to navigation
Jump to search
(Created page with '__NOTOC__ {{ModuleFunction|Sockets}} This function creates a socket. ==Syntax== <syntaxhighlight lang="lua"> socket sockOpen ( string hostname, int port) </syntaxhighlight> ===Required arguments=== * '''…') |
m (Fixed broken reference) |
||
(10 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
<pageclass class="#AA7592" subcaption="Sockets Module"></pageclass> | |||
__NOTOC__ | __NOTOC__ | ||
{{ModuleFunction|Sockets}} | {{ModuleFunction|Sockets}} | ||
Line 5: | Line 6: | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
socket sockOpen ( string hostname, int port) | socket sockOpen ( string hostname, int port ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Required arguments=== | ===Required arguments=== | ||
* '''hostname:''' The DNS or IP to connect to e.g. "www.google.com" | * '''hostname:''' The DNS or IP to connect to e.g. "www.google.com" | ||
* '''port:''' The port to bind the socket to e.g. 80 | * '''port:''' The port to bind the socket to e.g. 80 | ||
<!--* '''secure:''' A boolean specifying whether the connection should be secure (ssl)--> | |||
===Returns=== | ===Returns=== | ||
Line 18: | Line 20: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local root = getRootElement() | local root = getRootElement() | ||
local ircSocket = sockOpen("irc.gtanet.com") | local ircSocket = sockOpen("irc.gtanet.com",6667) | ||
addEventHandler("onSockOpened",root, | addEventHandler("onSockOpened",root, | ||
function (socket) | function (socket) | ||
if socket == ircSocket then | if socket == ircSocket then | ||
sockWrite(socket,"USER mta mta * | sockWrite(socket,"USER mta mta * :Bot\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 54: | Line 56: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==See | ==See Also== | ||
===Functions=== | |||
{{Modules/Sockets/Functions}} | {{Modules/Sockets/Functions}} | ||
===Events=== | |||
{{Modules/Sockets/Events}} | |||
[[pl:Modules/Sockets/sockOpen]] |
Latest revision as of 17:45, 15 January 2022
This function is provided by the external module Sockets. You must install this module to use this function. | |
This function creates a socket.
Syntax
socket sockOpen ( string hostname, int port )
Required arguments
- hostname: The DNS or IP to connect to e.g. "www.google.com"
- port: The port to bind the socket to e.g. 80
Returns
Returns userdata that represents the socket if you correct arguments were given, false otherwise.
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 * :Bot\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