HTTP network library

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Looking for a script that provides wrapper methods for connecting to some server and sending/receiving data via HTTP (and maybe other protocols, but they are not important). Other "useful" methods would be nice as well, whatever they might be (like handling different types of content?)


I am aware of a couple but I don't remember what they're called.


What is everyone else using?
 
Last edited by a moderator:

Dr.Yami

。◕‿◕。
Developer
Joined
Mar 5, 2012
Messages
1,003
Reaction score
757
First Language
Vietnamese
Primarily Uses
Other
Dr. Fomar has a script for HTTP connection.

I have seen a script for socket and a script for HTTP download too, but don't remember where I got them (socket was made by Zeus81 I think)

Socket:

#==============================================================================# ** Socket#------------------------------------------------------------------------------# Adds the possibility to send/receive messages to/from a server# Big thanks to Zeus81 (and to Nuki, too)#==============================================================================module Socket #-------------------------------------------------------------------------- # * Win32API #-------------------------------------------------------------------------- Socket = Win32API.new('ws2_32', 'socket', 'lll', 'l') Connect = Win32API.new('ws2_32', 'connect', 'ppl', 'l') Close = Win32API.new('ws2_32', 'closesocket', 'p', 'l') Send = Win32API.new('ws2_32', 'send', 'ppll', 'l') Recv = Win32API.new('ws2_32', 'recv', 'ppll', 'l') Inet_addr = Win32API.new('ws2_32', 'inet_addr', 'p', 'l') Htons = Win32API.new('ws2_32', 'htons', 'l', 'l') Shutdown = Win32API.new('ws2_32', 'shutdown', 'pl', 'l') #-------------------------------------------------------------------------- # * Constants #-------------------------------------------------------------------------- AF_INET = 2 SOCK_STREAM = 1 #-------------------------------------------------------------------------- # * Singleton #-------------------------------------------------------------------------- class << self #-------------------------------------------------------------------------- # * Creating the SOCKADDR_IN #-------------------------------------------------------------------------- def sockaddr_in(host, port) sin_family = AF_INET sin_port = Htons.(port) in_addr = Inet_addr.(host) sockaddr_in = [sin_family, sin_port, in_addr].pack('sSLx8') return sockaddr_in end #-------------------------------------------------------------------------- # * Creating the Socket #-------------------------------------------------------------------------- def socket() return Socket.(AF_INET, SOCK_STREAM, 0) end #-------------------------------------------------------------------------- # * Connecting #-------------------------------------------------------------------------- def connect_sock(sock, sockaddr) if Connect.(sock, sockaddr, sockaddr.size) == -1 p "Impossible to connect" return end p "Successfully connected" end #-------------------------------------------------------------------------- # * Connecting #-------------------------------------------------------------------------- def connect(host = "127.0.0.1", port = 9999) sockaddr = sockaddr_in(host, port) sock = socket() connect_sock(sock, sockaddr) return sock end #-------------------------------------------------------------------------- # * Sending data #-------------------------------------------------------------------------- def send(socket, data) value = Send.(socket, data, data.length, 0) if value == -1 p "Failed to send" shutdown(socket, 2) return false end p "Successfully sent" return true end #-------------------------------------------------------------------------- # * Receiving data #-------------------------------------------------------------------------- def recv(socket, len = 256) buffer = [].pack('x'+len.to_s) value = Recv.(socket, buffer, len, 0) return buffer.gsub(/\x00/, "") if value != -1 return false end #-------------------------------------------------------------------------- # * Stops the emission #-------------------------------------------------------------------------- def shutdown(socket, how) Shutdown.(socket, how) end #-------------------------------------------------------------------------- # * Closes the connection #-------------------------------------------------------------------------- def close(socket) Close.(socket) end end end
HTTP Download:

Code:
module Berka  module NetError    ErrConIn="Unable to connect to Internet"    ErrConHttp="Unable to connect to the Server"    ErrTranHttp="Http Download is failed"    ErrNoFile="No file to be download"    ErrMkdir="Unable to create a new directory"        HTTPSync=false #true: the game is faster but download are very slower:                   #false: 20secs & true: 1min  endendinclude Berka::NetErrormodule Net  W='wininet'  SPC=Win32API.new('kernel32','SetPriorityClass','pi','i').call(-1,128)  IOA=Win32API.new(W,'InternetOpenA','plppl','l').call('',0,'','',0)  IC=Win32API.new(W,'InternetConnectA','lplpplll','l')  print(ErrConIn)if IOA==0  module HTTP    IOU=Win32API.new(W,'InternetOpenUrl','lppllp','l')    IRF=Win32API.new(W,'InternetReadFile','lpip','l')    ICH=Win32API.new(W,'InternetCloseHandle','l','l')    HQI=Win32API.new(W,'HttpQueryInfo','llppp','i')    module_function    def sizeloaded(i='');@read[i];end    def transfered;@dloaded;end    def transfers;@dls;end    def progress(i='');(@read[i].to_f/@size[i]*100);end    def loaded?(i='');@read[i]>=@size[i]rescue nil;end    def temps(i='');@tps[i]if loaded?(i);end    def size(i='');@size[i];end    def download(url,int='./')      @dloaded||=0;@dls||={};@i||=-1;@size||={};@read||={};@tps={}      a=url.split('/');serv,root,fich=a[2],a[3..a.size].join('/'),a[-1]      print(ErrNoFile)if fich.nil?      @dls[fich]=Thread.start(url,int){|url,int|txt='';t=Time.now      ErrConHttp if(e=IC.call(IOA,serv,80,'','',3,1,0))==0        f=IOU.call(IOA,url,nil,0,0,0)      HQI.call(f,5,k="\0"*1024,[k.size-1].pack('l'),nil)      @read[fich],@size[fich]=0,k.delete!("\0").to_i      loop do        buf,n=' '*1024,0        r=IRF.call(f,buf,1024,o=[n].pack('i!'))        n=o.unpack('i!')[0]        break if r&&n==0        txt<<buf[0,n]        @read[fich]=txt.size        sleep(0.001)if HTTPSync      end      (File.open(int+fich,'wb')<<txt).close      @dloaded+=@read[fich]      ICH.call(f);sleep(0.01)      @tps[fich]=Time.now-t}    end  endend
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
LINK

That allows for both GET and POST http requests, also a demo on how to set it up :)

Edit:

Came across this post and figured i would add THIS
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,848
Messages
1,016,974
Members
137,562
Latest member
visploo100
Top