Jump to content

Autoit-Socket-IO - Networking in AutoIt made simple!


Recommended Posts

Version 2.x.x and 3.x.x has been moved to branch 3.x

About Autoit-Socket-IO

Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.

I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!

Key features

  • Simple API
  • 99% data-type serialization thanks to Autoit-Serialize
  • Can easily be extended with your own functionality thanks to Autoit-Events
  • "Educational" examples
  • Data encryption thanks to _<Crypt.au3>

Limitations

  • Speed. This UDF will sacrifice some speed for convenience

Getting started

  • Download the script from AutoIt or pull it from the official github repo git@github.com:tarreislam/Autoit-Socket-IO.git and checkout the tag 4.0.0-beta
  • Check out the documentaion
  • Take a look in the examples/ folder

Changelog

To see changes from 3.x.x and 2.x.x please checkout the 3.x branch

Version 4.0.0-beta (This update break scripts.)

  • Code base fully rewritten with Autoit-Events and decoupled to improve code quality and reduce bloat.
  • The new UDF is very different from 3.x.x so please checkout the UPGRADE guide to fully understand all changes
  • Added new documentation documentaion

Success stories

Since December 2017-now I have used version 1.5.0 in an production environment for 150+ clients with great success, the only downtime is planned windows updates and power outages.

 

Newest version (2020-09-15!)

 

Older versions (Not supported anymore)

Autoit-Socket-IO-1.0.0.zip Autoit-Socket-IO-1.1.0.zip Autoit-Socket-IO-1.3.0.zip Autoit-Socket-IO-1.4.0.zip Autoit-Socket-IO-1.5.0.zip

Autoit-Socket-IO-2.0.0.zip

Edited by tarretarretarre
Update
Link to comment
Share on other sites

It looks interesting.
I have to testify.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

Hello!

I have updated the UDF with a new features and improvements

  • Version 1.0.0 (This update DOES NOT break scripts)
  • Added data encryption (Using Autoit's UDF Crypt.au3) See more at _Io_EnableEncryption
  • Added new method _Io_Disconnect which can be used with both servers and clients
  • Improved package-handling to increase performance
  • Increased the limit of Broadcasted/Emit parameters from 10 to 16
Link to comment
Share on other sites

Hi again!

This will probably be the last update in a while, since i think i covered the most interesting parts from Socket.io.

Version 1.1.0 (This update DOES NOT break scripts)

  • Fixed bug when Emitting / Broadcasting without any parameters causing a $fCallback crash
  • Optimized Package-handling once again.
  • Added 1D-Array support (Endless nestning).
  • Added Subscriptions (See _Io_Subscribe _Io_Unsubscribe and _Io_BroadcastToRoom).
  • Added new example for subscriptions (Be sure to use different room names when joining with clients)
  • Added Unit testing (See Tests\Runner.au3 and Tests\Tests.au3, to run tests you need a udf found here: https://github.com/tarreislam/Autoit-Unit-Tester)
Link to comment
Share on other sites

So I thought!

Here is a fast-forward update

Version 1.3.0 (This update DOES NOT break scripts)

  • Got rid of unnecessary Redims with sockets and subscriptions in the main loop (This increased write performence greatly)
  • Changed $iMaxDeadSocketsBeforeTidy from 100 to 1000
  • Changed _Io_setRecvPackageSize($nPackageSize = 2048) to _Io_setRecvPackageSize($nPackageSize = 4096) because 2017.
  • Added Tests for both subscriptions and the automatic TidyUp
  • Added a new server method: _Io_getMaxConnections
  • Added a new server method: _Io_getMaxDeadSocketsCount
  • Added a fifth parameter to the _Io_Listen method called $iMaxConnections which defaults to 100000. If the iMaxConnection + 1 user connects, they will be instantly disconnected.
  • Added a parameter to _Io_Disconnect called $socket which defaults to null. If the iMaxConnections + 1 client connects, they will be instantly disconnected.

Version 1.2.0 (This update DOES NOT break scripts)

  • Added an option to set the packet-size of TCP-transports, see _Io_setRecvPackageSize
  • Got rid of unnecessary StringLen's in _Io_loop
  • Changed __Io_TidyUp to _Io_TidyUp and added it to the public Api reference list.
  • Changed $iMaxDeadSocketsBeforeTidy default value from 1000 to 100 and added an option to disable it, read more at _Io_Listen
  • Changed $bAutoReconnect from False to True.
  • Fixed gitignore epicZ fail
  • Improvemend Documentation
Link to comment
Share on other sites

  • 2 weeks later...

Ok I made it :

Serveur

#AutoIt3Wrapper_Change2CUI=Y
#include ".\UDF\socketIO.au3"

;Start server
Global $socket = _Io_Listen(8080)

If Not @error Then
    ConsoleWrite("Listening on port 8080" & @CRLF)
Else
    ConsoleWrite("Failed to open socket:" & @error & @CRLF)
    Exit
EndIf

; -------------
;   All events are registered here
; -------------
_Io_on("join", ClientJoined)
_Io_on("message", Message)

; Start main loop
While _Io_Loop($socket)
WEnd

Func Message(ByRef $socket, $name, $ip , $message)
    ; Transit message the message
    ;_Io_BroadcastToAll($socket, "message", $name, $message)
    _Io_BroadcastToAll($socket, $ip, $name & ": " & $message)
    ConsoleWrite($name & ": " & $message & @CRLF)
EndFunc

Func ClientJoined(ByRef $socket, $name)
    ConsoleWrite("Client joined: " & $name & @CRLF)
EndFunc

client 1 who send message to client 2 with the right ip

#AutoIt3Wrapper_Change2CUI=Y
#include ".\UDF\socketIO.au3"
#include ".\UDF\Toast.au3"
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; Its recommended to always use Gui events to remove the programs main focus from updating the GUI
Opt("GUIOnEventMode", 1)

Global $username = @UserName

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 272, 154, 636, 373)
Global $Input1 = GUICtrlCreateInput("IP", 32, 16, 217, 21)
Global $Input2 = GUICtrlCreateInput("Message", 32, 48, 217, 21)
Global $Button1 = GUICtrlCreateButton("Button1", 88, 88, 97, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Connect to server
Global $socket = _Io_Connect(@IPAddress1, 8080, True)
If @error Then
    ConsoleWrite("Failed to open socket:" & @error & @CRLF)
    Exit
EndIf

; Register gui events
GUICtrlSetOnEvent($Button1, "SendMessage")
GUISetOnEvent($GUI_EVENT_CLOSE, "_quit")

; Notify server that we are here!
_Io_Emit($socket, "join", $username)

; Start main loop
While _Io_Loop($socket)
WEnd

; Gui functions
Func SendMessage()
    ; Send message
    _Io_Emit($socket, "message", $username , GUICtrlRead($Input1) , GUICtrlRead($Input2))
    ; Clear input
    GUICtrlSetData($Input2, "")
EndFunc

Func _quit()
    Exit
EndFunc

client 2

#include ".\UDF\socketIO.au3"
#include ".\UDF\Toast.au3"

Global $aRet[2]

; Connect to server
Global $socket = _Io_Connect("192.168.1.254", 8080, True)
If @error Then
    ConsoleWrite("Failed to open socket:" & @error & @CRLF)
    Exit
EndIf

; -------------
;   All events are registered here
; -------------
_Io_on("192.168.1.1", callback);ip client

; Start main loop
While _Io_Loop($socket)
WEnd

Func callback(ByRef $socket, $message)
    ;MsgBox(0, "The Client", $message)
    $aRet = _Toast_Show(0, "Form client 1", $message, 5)
    _Toast_Hide()
EndFunc   ;==>callback_serverHasGreetedUs

 

Link to comment
Share on other sites

Hi and thank you!

I see what you are trying to do, instead of binding an event for each client, you could do something like this instead:

 

Note: I use UDF global variables (which is bad practise), theres no guarantee that this will be working in the future and I will address this issue for the next update.

 

In the server, add this:

_Io_On("private message", Callback_PrivateMessage)


Func Callback_PrivateMessage(ByRef $socket, $message, $to_ip)

    ; Get the person whos trying to send an private message
    Local $sender_ip = _Io_socketGetProperty($socket, "ip")

    ; Loop through each socket stored on server
    For $i = 1 To $__g_io_extended_sockets[0]
        ; Open the ExtendedSocket array
        Local $aExtendedSocket = $__g_io_extended_sockets[$i]
        ; Grab the socket
        Local $client_socket = $aExtendedSocket[0]
        ; Grab the ip
        Local $client_ip = $aExtendedSocket[1]

        ; Compare the ip that the client sent us (Also ignore dead sockets
        If $to_ip == $nClientIp AND $__g_io_sockets[$i] <> Null  Then
            ; Send
            _Io_Emit($client_socket, "private message", $sender_ip, $message)

            ; Remove this exitloop if you want continue search after the first result
            ExitLoop
        EndIf

    Next
EndFunc

 

On the client, add this:

_Io_On("private message", Callback_PrivateMessage)

Func Callback_PrivateMessage(ByRef $socket, $sender_ip, $message)

    MsgBox(0, "Private message from " & $sender_ip, $message)

EndFunc

 

Too send private messages from the client do like so:

_Io_Emit($socket, "private message", "Secret message", "192.168.0.10")

 

 

I have not tested this code because im not home

Link to comment
Share on other sites

Thanks I get it work :

Server :

#AutoIt3Wrapper_Change2CUI=Y
#include ".\UDF\socketIO.au3"

;Start server
Global $socket = _Io_Listen(8080)

If Not @error Then
    ConsoleWrite("Listening on port 8080" & @CRLF)
Else
    ConsoleWrite("Failed to open socket:" & @error & @CRLF)
    Exit
EndIf

_Io_on("join", ClientJoined) ;réaction a l'evenement "join"
_Io_On("private message", Callback_PrivateMessage)

; Start main loop
While _Io_Loop($socket)
WEnd

Func ClientJoined(ByRef $socket, $name)
    ConsoleWrite("Client joined: " & $name & @CRLF)
EndFunc

Func Callback_PrivateMessage(ByRef $socket,  $to_ip , $message)
    ConsoleWrite("Private Message " & $message & " vers " & $to_ip & @CRLF)
    ; Get the person whos trying to send an private message
    Local $sender_ip = _Io_socketGetProperty($socket, "ip")

    ; Loop through each socket stored on server
    For $i = 1 To $__g_io_extended_sockets[0]
        ; Open the ExtendedSocket array
        Local $aExtendedSocket = $__g_io_extended_sockets[$i]
        ; Grab the socket
        Local $client_socket = $aExtendedSocket[0]
        ; Grab the ip
        Local $client_ip = $aExtendedSocket[1]

        ; Compare the ip that the client sent us (Also ignore dead sockets
        If $to_ip = $client_ip AND $__g_io_sockets[$i] <> Null  Then
            ; Send
            _Io_Emit($client_socket, "private message", $sender_ip, $message)

            ; Remove this exitloop if you want continue search after the first result
            ExitLoop
        EndIf
    Next
EndFunc

client who send private message

#include ".\UDF\socketIO.au3"
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; Its recommended to always use Gui events to remove the programs main focus from updating the GUI
Opt("GUIOnEventMode", 1)

Global $username = @UserName

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 272, 154, 636, 373)
Global $Input1 = GUICtrlCreateInput("IP", 32, 16, 217, 21)
Global $Input2 = GUICtrlCreateInput("Message", 32, 48, 217, 21)
Global $Button1 = GUICtrlCreateButton("Button1", 88, 88, 97, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Connect to server
Global $socket = _Io_Connect("192.168.1.10", 8080, True)
If @error Then
    MsgBox(0,"Failed to open socket:",@error & @CRLF,5)
    Exit
EndIf

; Register gui events
GUICtrlSetOnEvent($Button1, "SendMessage")
GUISetOnEvent($GUI_EVENT_CLOSE, "_quit")

; Notify server that we are here!
_Io_Emit($socket, "join", $username);envoi au serveur l'evenement "join" qui indique que l'utilisateur est connecté

; Start main loop
While _Io_Loop($socket)
WEnd

; Gui functions
Func SendMessage()
    ; Send message
    _Io_Emit($socket, "private message", GUICtrlRead($Input1) , GUICtrlRead($Input2))
    ; Clear input
    GUICtrlSetData($Input2, "")
EndFunc

Func _quit()
    Exit
EndFunc

client who receive

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include ".\UDF\socketIO.au3"

Global $aRet[2]

; Connect to server
Global $socket = _Io_Connect("192.168.1.10", 8080, True)
If @error Then
    MsgBox(0,"Failed to open socket:",@error & @CRLF,5)
    Exit
EndIf

_Io_On("private message", Callback_PrivateMessage)
; Notify server that we are here!
_Io_Emit($socket, "join", @UserName & @CRLF)

; Start main loop
While _Io_Loop($socket)
WEnd

Func Callback_PrivateMessage(ByRef $socket, $sender_ip, $message)
    MsgBox(0, "Private message from " & $sender_ip, $message)
EndFunc

 

Link to comment
Share on other sites

You can find some examples at GitHub: https://github.com/tarreislam/Autoit-Socket-IO

ccqL8C7.png

P.S

51 minutes ago, Trong said:

UPD 

I think you mean UDP :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Hi!

I will not officially support UDP, but technically you could replace all TCP commands with UDP.

Regarding filetransfers: 

Open the file with https://www.autoitscript.com/autoit3/docs/functions/FileOpen.htm  $FO_BINARY mode

Split and transmit each chunk of the file until you reach the end.

 

I will probably add an example for the next update.

Link to comment
Share on other sites

Yo, here is a medium-big update containing new features, examples and optimizations.

 

I Decided to NOT include any Filetransfer examples, I rather create a separate thread about it.

 

Version 1.4.0 (This update DOES NOT break scripts)

  • Added a new server method: _Io_getSockets which will return an array of all sockets. See more in the doc
  • Added a banning-system, see more at: _Io_getBanlist, _Io_Ban, _Io_Sanction, _Io_IsBanned
  • Added a new default event for clients banned. See more at default events
  • Added two new client and server methods _Io_setEventPreScript And _Io_setEventPostScript. The intent for these is to not DRY when doing debug \ tasks that requires to be ran before or after events.
  • Added a new client and server method _Io_ClearEvents.
  • Added a third optional parameter to _Io_On called $socket, you may only pass the socket returned from _Io_Listen or _Io_Connect. The intent for this change is to allow for server + client in the same envoirment.
  • Added a second parameter to _Io_Loop called $WhoAmI which should used with the new enums $_IO_SERVER and $_IO_CLIENT. The intent for this change is to allow for server + client in the same envoirment.
  • Added a new client method _Io_TransferSocket.
  • Added a new server method _Io_getActiveSocketCount.
  • Optimations, avoiding Redims and unnecessary nested arrays as good as possible etc.
Link to comment
Share on other sites

  • 1 month later...

For group chats, look at Subscriptions. Example here: https://github.com/tarreislam/Autoit-Socket-IO/tree/master/Example - Chat with subscriptions

Client to client still have to go through the server, look at post #9. I will add an example for a future release :)

Link to comment
Share on other sites

Hello. I am not dead!

I suggest that everyone that uses this UDF in production, updates to this version.

Version 1.5.0 (This update DOES NOT break scripts)

  • Added AutoIt like docs under Docs\. The docs are generated so its a 1 to 1 reflection of the UDF headers. Print of documentation
  • Added a production ready server example.
  • Added a new method: _Io_DevDebug which will display useful information when debugging.
  • Added a new method: _Io_SetMaxRecvPackageSize which defaults to whatever _Io_setRecvPackageSize is set to.
  • Added a new method: _Io_setOnPrefix which defaults to _On_
  • Added a new default client & server event called flood. Flood occurs when exceeding the $__g_io_nMaxPacketSize. $__g_io_nMaxPacketSize is set by _Io_SetMaxRecvPackageSize
  • Fixed the 16 parameter limit when sending data with _Io_Emit, _Io_Broadcast, _Io_BroadcastToAll and _Io_BroadcastToRoom. This works on the same premise that AutoIt's Call Does
  • Fixed a TRUNCATION problem when receiving packages which could cause crashes!
  • Fixed a programming error which caused $__g_ionPacketSize to reset to default 4096 if _Io_Connect or _Io_listenwere called after _Io_setRecvPackageSize
  • Fixed _Io_setEventPreScript and _Io_setEventPostScript They didnt work. Lol.
  • Changed how events are fired so the client cannot crash the server by sending the wrong number of parameters (This also allows for optional parameters on callbacks)
  • Changed _Io_On. The second parameter $fCallback can now be set to null. Doing this, the function assumes that the callback is: _On_<eventName>.
Edited by tarretarretarre
Link to comment
Share on other sites

  • 4 weeks later...

This UDF cannot handle such throughput with the current design. However, im working on an new UDF which is designed for high throughput (Aka the thing you are asking for)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...