there was an update! sockets_listen is now socket_listen, and there is a new function regex_match
TL;DR
the new websockets are here meaning the old wiki is outdated
these can connect to any TCP server including IRC, Telnet, and you can sepecify your own user agent when visiting sites in HTTP
first you have to wrap the peripheral and initiate a connection for example an IRC server:
local s=peripheral.wrap("top")
local h=s.socket_create("irc.esper.net",6667)
the string can be an ip adress or something DNS can resolve and the second argument is the portsocket_create returns a handle wich you will be using in other functions
s.socket_setAutoFlush(h,true) s.sockets_listen(h)socket_setAutoFlush will automatically call socket_flush when you write to it
sockets_listen will enable the "socket_line" event
now we can write to the socket, on irc we have to specify our user info:
s.socket_write(h,"NICK Potato\n") s.socket_write(h,"USER Potato NotWebchat irc.esper.net :Potatoes are fun\n")you HAVE to put \n after or the server will not process it, unless you are sending POST content, in wich case you have to flush afterwards
now we have to read data from the server:
local p={os.pullEvent("socket_line")}
the structure of the event is:{"socket_line",side,h,data}
so to respond to a PING request from the server you do this:if p[1]=="socket_line" and p[3]==h then if string.sub(p[4],1,5)=="PING " then s.socket_write(h,"PONG "..string.sub(p[4],6).."\n") end end
and when your done you can close it!
s.socket.write(h,"QUIT\n") s.socket_close(h)
or if your server disconnects there is this event
{"socket_connectionClosed",side,h}
a side note, if you are using a VPS to host your server your remote ports are probably being blocked meaning irc probably wont work
Edited by PixelToast, 03 December 2013 - 02:03 PM.












