This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
PixelToast's profile picture

Webinterfaces From Kilobyte's Peripherals

Started by PixelToast, 11 June 2013 - 10:18 AM
PixelToast #1
Posted 11 June 2013 - 12:18 PM
this is for Kilobyte's peripherals, grab it here

there was an update! sockets_listen is now socket_listen, and there is a new function regex_match

TL;DR
Spoiler

handle=s.socket_create(server,port)
s.socket_setAutoFlush(handle,true)
s.socket_flush(handle)
s.sockets_listen(handle) -- enables "socket_line"
s.socket_write(handle)
s.socket_close(handle)
event "socket_line",h,side,data

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 port
socket_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 on 03 December 2013 - 01:03 PM
Tiin57 #2
Posted 11 June 2013 - 02:02 PM
AFAIK the IRC endline is '\n\r' or '\r\n'.
PixelToast #3
Posted 11 June 2013 - 02:28 PM
just \n works for me, \r\n is HTTP headers

print("HTTP/1.1 200/OK\r\nServer: Potato\r\n")
print("Content-Type: text/html\r\n\r\n")
Cruor #4
Posted 12 June 2013 - 03:38 AM
The protocol specifies that \r\n is to be used, some tend to strip the \r though.
Kilobyte #5
Posted 12 June 2013 - 02:52 PM
Wait, is it really sockets_listen? *checks repo* *swears since he forgot to commit*
meh, if it is really sockets_listen, then thats a bug and will be fixed once i get my computer back. i will prob keep sockets_listen for at least one release though for backwards compatibility

Also, in the PONG line you forgot the newline ;)/>

Otherwise very good work. +1 Cookie for you :3

Also, you might wonder why there is socket(s)_listen(). basicly i am going to add hooks to read from the stream => you have binary support. it will probably be something like socket_read(handle, amountOfBytes). it won't be a blocking call. so if there is less data received than you want to read it will only return what it already read.
PixelToast #6
Posted 12 June 2013 - 06:50 PM
any way to receive things coming into a socket? (i thought it was socket_read)
i was gona make a chat server with this but didnt know how
ElvishJerricco #7
Posted 16 June 2013 - 02:03 PM
Just FYI, calling this Websockets is a bit misleading since there is a socket protocol called WebSockets that's basically just an AJAX replacement for web apps by allowing the browser to connect via a special socket.
gudenau #8
Posted 22 June 2013 - 08:23 PM
Is it just me or did sockets_listen disappear?
CoderPuppy #9
Posted 22 June 2013 - 08:41 PM
I think it got renamed to socket_listen.
FNCPro #10
Posted 23 June 2013 - 03:08 PM
It didn't work… WHY???
PixelToast #11
Posted 30 June 2013 - 06:55 PM
o.o strange, the link kilo is giving 4.0.23 but the link says 4.0.26
im using 4.0.26 so this might not work properly .-. ill try to contact him but he seems to be missing
if you really want i can send you the 4.0.26 version


EDIT:
fixed, thanks kilo
Kilobyte #12
Posted 02 July 2013 - 01:51 PM
Sorry, those bugs were my bad :(/>

Its fixed now. any further stuff can be reported on issue tracker. It works now


Just FYI, calling this Websockets is a bit misleading since there is a socket protocol called WebSockets that's basically just an AJAX replacement for web apps by allowing the browser to connect via a special socket.
I totally agree
rickydaan #13
Posted 08 July 2013 - 08:03 AM
JSONAPI + this.peripheral (Websockets) + Minecraft + Bukkit + CC ==

COMPUTERCRAFT SERVER MANAGEMENT PROGRAM :D/>
PixelToast #14
Posted 10 July 2013 - 10:22 PM
JSONAPI + this.peripheral (Websockets) + Minecraft + Bukkit + CC ==

COMPUTERCRAFT SERVER MANAGEMENT PROGRAM :D/>
was gona make a RCON program but webinterfaces dont support UDP :/
though its relatively eays to make a tcp - udp bridge in normal lua
Kilobyte #15
Posted 26 August 2013 - 06:06 AM
Should I add udp? :)/>
PixelToast #16
Posted 26 August 2013 - 08:37 AM
:D/> would be awesome
also add a way to read only a certain amount of bytes per event
Kilobyte #17
Posted 26 August 2013 - 12:26 PM
That's also planned :)/> meh, I'm on vacation atm so can't code, but I will be able to next week.