That's because your browser speaks the http protocol language a bit more in depth than your script.
Your request is timing out because you're not sending an empty crlf at the end, try sending it.
Note and tips: typically people /sockclose the socket name before attempting to /sockopen as that will create an error if the socket name is already in use, $sockerr should be checked prior any /sockwrite inside on sockopen, not after.
Inside on sockread, in practice people check $sockerr once before any /sockread, but won't check $sockerr again if they use /sockread again later on.
The help file says that $sockerr should be checked after any socket command and before processing events, but in practice scripts are typically only checking $sockerr before processing events, there are no known cases of $sockerr being UNset before executing a command and set after.
You can get more info about socket error with $sock(socketname).wsmsg
Try this
alias Example {
sockclose example
sockopen Example github.com 80
; Check if there was an error.
; in practice this check is not required, if anyone know how to get $sockerr set after executing a /sockopen command that did not result in a script error, let me know
if ($sockerr) {
echo -ag There was an error: $sockerr
halt
}
}
; Listen for the on SOCKOPEN event.
on *:SOCKOPEN:Example:{
if ($sockerr) {
echo -ag there was an error: $sockerr : $sock($sockname).wsmsg
; we usually /return event, not halt them unless you specifically want to halt, when we want to stop processing)
return
}
; Write a GET request.
sockwrite -nt $sockname GET / HTTP/1.1
sockwrite -n $sockname
; Check if there was an error. again this check is not required in practice, same as above
if ($sockerr) {
echo -ag There was an error: $sockerr
halt
}
}
; Listen for the on SOCKREAD event.
on *:SOCKREAD:Example:{
; Check if there was an error.
if ($sockerr) {
echo -ag There was an error: $sockerr
halt
}
; Create a local variable to hold the data.
var %i
; Read exactly one line from the received buffer, the data being read is put into the local variable.
sockread %i
;if data was received but does not contain a new line, sockread won't read anything and $sockbr is 0, you need to stop processing and wait for the event to trigger again with more data, hopefully with a newline in it.
if ($sockbr == 0) return
; if a line has been found, Print it
echo -ag Data = %i
;not sure what you're trying to do with %a = %i here.
%a = %i
}
Note that reading line by line like this will not read the last line if it does not end with a line ending sequence (crlf or lf), in this case you can either use the content length header to figure out if you need to read by forcing the data to be read with /sockread -f, or if your socket is not being kept alive and is closing itself after the request is done, you can just read with sockread -f from the on sockclose event.