sockets?
#1
Im currently devloping a plugin to connect to a socket server i just wanted to know if there is a socket module or would i have to use luasockets dll?
Reply
Thanks given by:
#2
The engine of MCServer is not LUA, so you have to use Java, I think. o.O
Reply
Thanks given by:
#3
(02-28-2011, 01:56 AM)Jockie Wrote: The engine of MCServer is not LUA, so you have to use Java, I think. o.O

I think he wants the plugin to gather some data from the server.
You would need a lua module included by MCServer for this to work,
or I think loading the luasockets module from within lua.

greets,
Tybor
Reply
Thanks given by:
#4
I already created something for this, and exported it to Lua
Code:
class cTCPLink
{

public:
        cTCPLink();
        ~cTCPLink();
        bool Connect( _cstring  a_Address, unsigned int a_Port );
        int Send( _cstring  a_Data, unsigned int a_Size, int a_Flags = 0 );
        int SendMessage( const _cstring  a_Message, int a_Flags = 0 );
        void CloseSocket();
protected:
        virtual void ReceivedData( char a_Data[256], int a_Size ) = 0;
};

It's not documented anywhere. You basically need to 'extend' it like you extend cPlugin. You need this piece of code for that:
Code:
local TCPLink = {}
TCPLink.__index = TCPLink

function TCPLink:new()
   local t = {}
   setmetatable(t, TCPLink)
   local w = Lua__cTCPLink:new()
   tolua.setpeer(w, t)
   w:tolua__set_instance(w)
   return w
end

Then you can inherit the ReceivedData function like so:
Code:
TCPLink:ReceivedData( Data, Size )
   -- Do stuffs with data
end
Reply
Thanks given by:
#5
Okay im a tad confused as i only ever use pure lua and gmod lua, So what exactly do i need to do with this code?
Reply
Thanks given by:
#6
Here's an example:
Code:
local TCPLink = {}
TCPLink.__index = TCPLink
function TCPLink:new()
   local t = {}
   setmetatable(t, TCPLink)
   local w = Lua__cTCPLink:new_local() -- new_local = Deleted by garbage collector
   tolua.setpeer(w, t)
   w:tolua__set_instance(w)
   return w
end

TCP = TCPLink:new()
if( TCP:Connect("www.mc-server.org", 80) == true ) then
    Log("Sending message")
    TCP:SendMessage("GET http://www.mc-server.org/\n")
end

function TCPLink:ReceivedData( Data, Size )
    Log("Received data: " .. Size )
    String = string.sub( Data, 0, Size )
    Log( String )
end
Reply
Thanks given by:
#7
Thanks dude, i can now start working on my plugin.
Reply
Thanks given by:
#8
On trying your example it crashes after data is recevied, Any ideas?
Reply
Thanks given by:




Users browsing this thread: 5 Guest(s)