DigiTechs wrote:
What method should I use for sending messages to the server? POST or GET? I know GET is to 'GET' somthing, but I don't really know.. And you said the messages are sent in base255? That's not good.. because I don't have a base255 plugin in my Lua installation, only Base64...
You don't use HTTP, you use raw sockets. It's not base255, you use something like string.char() and some bitshifting. The protocol is described here (and there's various example code for other languages like Python that you might find useful, too).
Edit: looks like you need something like: struct.pack('>I4', size)
Last edited by blob8108 (2012-11-25 06:23:41)
Offline
blob8108 wrote:
DigiTechs wrote:
What method should I use for sending messages to the server? POST or GET? I know GET is to 'GET' somthing, but I don't really know.. And you said the messages are sent in base255? That's not good.. because I don't have a base255 plugin in my Lua installation, only Base64...
You don't use HTTP, you use raw sockets. It's not base255, you use something like string.char() and some bitshifting. The protocol is described here (and there's various example code for other languages like Python that you might find useful, too).
Edit: looks like you need something like: struct.pack('>I4', size)
Meh. the Socket API in Lua supports MIME.. I'm going to use that. I've read it, it looks pretty close to what's needed.
Offline
DigiTechs wrote:
What method should I use for sending messages to the server? POST or GET? I know GET is to 'GET' somthing, but I don't really know.. And you said the messages are sent in base255? That's not good.. because I don't have a base255 plugin in my Lua installation, only Base64...
It's base 256, and you can just do it by converting numbers to character codes.
length = string.char( math.floor(n / 0x1000000) % 0x100, math.floor(n / 0x10000) % 0x100, math.floor(n / 0x100) % 0x100, n % 0x100)
EDIT: Removed the bitwise operators.
Last edited by nXIII (2012-11-25 11:20:01)
Offline
nXIII wrote:
DigiTechs wrote:
What method should I use for sending messages to the server? POST or GET? I know GET is to 'GET' somthing, but I don't really know.. And you said the messages are sent in base255? That's not good.. because I don't have a base255 plugin in my Lua installation, only Base64...
It's base 256, and you can just do it by converting numbers to character codes.
Code:
length = string.char( math.floor(n / 0x1000000) % 0x100, math.floor(n / 0x10000) % 0x100, math.floor(n / 0x100) % 0x100, n % 0x100)EDIT: Removed the bitwise operators.
Ok. I'll use that.
Offline
You don't actually use base255. The easiest way is to do it like this:
0x00 0x00 0x00 length. Length must be one byte, and the 0s are one byte, too!
Offline
bobbybee wrote:
You don't actually use base255. The easiest way is to do it like this:
0x00 0x00 0x00 length. Length must be one byte, and the 0s are one byte, too!
Ok.
I copied bits from the server code and recoded them in Lua so I could get the messages to send and recieve, but for some reason, I keep getting an error..
error wrote:
lua: echoclnt.lua:15: bad argument #1 to 'len' (string expected, got nil)
stack traceback:
[C]: in function 'len'
echoclnt.lua:15: in function 'addlen'
echoclnt.lua:25: in function 'send_broadcast'
echoclnt.lua:32: in main chunk
[C]: ?
Here's the code I'm using:
local socket = require("socket") host = host or "69.164.139.192" port = port or 42001 if arg then host = arg[1] or host port = arg[2] or port end function addlen(commd) n = string.len(commd) length = string.char( math.floor(n / 0x1000000) % 0x100, math.floor(n / 0x10000) % 0x100, math.floor(n / 0x100) % 0x100, n % 0x100) return ""..tostring(length)..""..cmd.."" end function send_broadcast(name) msg = ('broadcast "{0}"'..string.format(name)) msg = addlen(message) assert(udp:send(message)) end host = socket.dns.toip(host) udp = assert(socket.udp()) assert(udp:setpeername(host, port)) print("Using remote host '" ..host.. "' and port " .. port .. "...") send_broadcast("<chat2") while 1 do line = io.read() if not line or line == "" then os.exit() end send_broadcast(line) dgram = assert(udp:receive()) print(dgram) end
I think the server is a TCP server, but looking at the code, I can't exactly tell.
Offline
function send_broadcast(name)
msg = ('broadcast "{0}"'..string.format(name))
msg = addlen(message)
assert(udp:send(message))
end
You send message to addled, not msg
Offline
DigiTechs wrote:
Does Scratch use TCP connection or UDP connection?
TCP.
Offline
bobbybee wrote:
function send_broadcast(name)
msg = ('broadcast "{0}"'..string.format(name))
msg = addlen(message)
assert(udp:send(message))
end
You send message to addled, not msg
?? I do not get a word of that.
Do you mean that I forgot somthing.. oh wait I did. Thanks for pointing out...
Here's my new code:
local socket = require("socket") host = host or "69.164.139.192" port = port or 42001 if arg then host = arg[1] or host port = arg[2] or port end function addlen(commd) n = string.len(commd) length = string.char( math.floor(n / 0x1000000) % 0x100, math.floor(n / 0x10000) % 0x100, math.floor(n / 0x100) % 0x100, n % 0x100) return ""..tostring(length)..""..cmd.."" end function send_broadcast(name) msg = ('broadcast "{0}"'..string.format(name)) msg = addlen(message) assert(udp:send(msg)) end host = socket.dns.toip(host) udp = assert(socket.udp()) assert(udp:setpeername(host, port)) print("Using remote host '" ..host.. "' and port " .. port .. "...") send_broadcast("<chat2") while 1 do line = io.read() if not line or line == "" then os.exit() end send_broadcast(line) dgram = assert(udp:receive()) print(dgram) end
Offline
blob8108 wrote:
DigiTechs wrote:
Does Scratch use TCP connection or UDP connection?
TCP.
So the server runs in TCP? Meep. Lua doesn't like me when using TCP connections, it only works when using the UDP connection.
Offline
OK, i got everything working, the only problem I have is the bot timing out:
Lua wrote:
lua: echoclnt.lua:30: timeout
stack traceback:
[C]: in function 'assert'
echoclnt.lua:30: in main chunk
[C]: ?
The code i'm using is: (I fixed UDP)
local socket = require("socket") host = host or "69.164.139.192" port = port or 42001 if arg then host = arg[1] or host port = arg[2] or port end function addlen(commd) n = string.len(commd) length = string.char( math.floor(n / 0x1000000) % 0x100, math.floor(n / 0x10000) % 0x100, math.floor(n / 0x100) % 0x100, n % 0x100) return ""..tostring(length)..""..cmd.."" end function send_broadcast(name) msg = ('broadcast "{0}"'..string.format(name)) msg = addlen(message) assert(tcp:send(msg)) end host = socket.dns.toip(host) tcp = assert(socket.tcp()) assert(tcp:connect(host, port)) print("Using remote host '" ..host.. "' and port " .. port .. "...") send_broadcast("<chat2") while 1 do line = io.read() if not line or line == "" then os.exit() end send_broadcast(line) dgram = assert(tcp:receive()) print(dgram) end
HMM?
Last edited by DigiTechs (2012-11-26 11:14:00)
Offline
OK, I changed the code a little. I found out some things were stupidly typed, so I corrected them. Here's my NEW code:
local socket = require("socket") host = host or "69.164.139.192" port = port or 42001 if arg then host = arg[1] or host port = arg[2] or port end function addlen(commd) n = string.len(commd) length = string.char( math.floor(n / 0x1000000) % 0x100, math.floor(n / 0x10000) % 0x100, math.floor(n / 0x100) % 0x100, n % 0x100) return ""..tostring(length)..""..commd.."" --EDIT: I realised 'cmd' is an invalid variable, so 'commd'. end function send_broadcast(name) msg = ('broadcast "'..string.format(name)..'"') msg = addlen(message) assert(tcp:send(msg)) end tcp = assert(socket.tcp()) assert(tcp:connect(host, port)) print("Using remote host '" ..host.. "' and port " .. port .. "...") send_broadcast("<chat2") while 1 do line = io.read() if not line or line == "" then os.exit() end send_broadcast(line) dgram = assert(tcp:receive()) print(dgram) end
And obviously with more code/less code, there's more errors!
error wrote:
lua: echoclnt.lua:23: timeout
stack traceback:
[C]: in function 'assert'
echoclnt.lua:23: in main chunk
[C]: ?
>Exit code: 1
Lua just likes to time out
Kinda not fair. Well, I suppose everything's not fair to some extent.
Last edited by DigiTechs (2012-11-26 11:36:55)
Offline
Scratch Wiki wrote:
Words and strings are encoded in UTF-8.
Note: ASCII is a subset of UTF-8).
Can someone explain this? I mean, I get what it says in ASCII is a subset of UTF-8, but I can't find a way to encode Lua strings into UTF-8 but I can find ASCII. Is there a way to turn ASCII into pure UTF-8?
Offline
DigiTechs wrote:
OK, I changed the code a little. I found out some things were stupidly typed, so I corrected them. Here's my NEW code:
Code:
local socket = require("socket") host = host or "69.164.139.192" port = port or 42001 if arg then host = arg[1] or host port = arg[2] or port end function addlen(commd) n = string.len(commd) length = string.char( math.floor(n / 0x1000000) % 0x100, math.floor(n / 0x10000) % 0x100, math.floor(n / 0x100) % 0x100, n % 0x100) return ""..tostring(length)..""..commd.."" --EDIT: I realised 'cmd' is an invalid variable, so 'commd'. end function send_broadcast(name) msg = ('broadcast "'..string.byte(name)..'"') msg = addlen(message) assert(tcp:send(msg)) end tcp = assert(socket.tcp()) assert(tcp:connect(host, port)) print("Using remote host '" ..host.. "' and port " .. port .. "...") send_broadcast("<chat2") while 1 do line = io.read() if not line or line == "" then os.exit() end send_broadcast(line) dgram = assert(tcp:receive()) print(dgram) endAnd obviously with more code/less code, there's more errors!
error wrote:
lua: echoclnt.lua:23: timeout
stack traceback:
[C]: in function 'assert'
echoclnt.lua:23: in main chunk
[C]: ?
>Exit code: 1Lua just likes to time out
Kinda not fair. Well, I suppose everything's not fair to some extent.
WAIIT........ I think I might call myself 'braindead' - I just found a MAJOR DERP in my code:
Major Derp wrote:
msg = ('broadcast "'..string.byte(name)..'"')
msg = addlen(message)
Do you see what it's doing? It's setting the 'msg' variable to 'broadcast "*valueof(string.byte(name))*"' and THEN to what addlen(message) returns.. *chipmunk voice deeper* Major derpstar!
Offline
I just updated my code. I think it should work a bit better now, because I discovered that string.byte() only returns the FIRST LETTER of the string in ASCII, so I hope this works.
local socket = require("socket") host = host or "69.164.139.192" port = port or 42001 if arg then host = arg[1] or host port = arg[2] or port end function addlen(commd) n = string.len(commd) length = string.char( math.floor(n / 0x1000000) % 0x100, math.floor(n / 0x10000) % 0x100, math.floor(n / 0x100) % 0x100, n % 0x100) return ""..tostring(length)..""..commd.."" end function send_broadcast(name) local endmsg = "" for i in name do endmsg = ""..endmsg..""..string.byte(name[i]).."" end msg = ('broadcast "'..string.byte(addlen(endmsg))..'"') assert(tcp:send(msg)) end tcp = assert(socket.tcp()) assert(tcp:settimeout(1)) assert(tcp:connect(host, port)) print("Using remote host '" ..host.. "' and port " .. port .. "...") send_broadcast("<chat2") while 1 do line = io.read() if not line or line == "" then os.exit() end send_broadcast(line) dgram = assert(tcp:receive()) print(dgram) end
Ok it doesn't. Apparently I can't cycle through the letters of a string a similar way to a table.
Last edited by DigiTechs (2012-11-26 12:14:18)
Offline
You want:
function send_broadcast(name) msg = ('broadcast "{0}"'..string.format(name)) msg = addlen(msg) assert(udp:send(msg)) end
Since the length is the length of the entire message that's being sent.
Offline
Magnie wrote:
You want:
Code:
function send_broadcast(name) msg = ('broadcast "{0}"'..string.format(name)) msg = addlen(msg) assert(udp:send(msg)) endSince the length is the length of the entire message that's being sent.
Ok then, I was just wondering because Lua had different string concatetions (e.e I can never spell that right, unless I just did ) than in Python.
Offline
Hmmmmmmmmmm.. I think I might call this thread 'dead' - No on's replying at all and I seem to be the only one who does. But, I am still working on my Lua bot! I still haven't fixed the timeouts, but I tried it when connecting to a localhost server and it worked fine. I added some commands, too. If anyone sees me messing about with a chatter with ANY DigiBot name, it's probbably mine. But I have a special syntax for my DigiBot bots.
Offline
DigiTechs wrote:
Hmmmmmmmmmm.. I think I might call this thread 'dead' - No on's replying at all and I seem to be the only one who does. But, I am still working on my Lua bot! I still haven't fixed the timeouts, but I tried it when connecting to a localhost server and it worked fine. I added some commands, too. If anyone sees me messing about with a chatter with ANY DigiBot name, it's probbably mine. But I have a special syntax for my DigiBot bots.
It's in stasis at the moment.
Hopefully this thread will become much more active once I get LSTC Green out and I release Chat.PY 4.
Offline
laser314 wrote:
Is chat.py 3.0 working yet?
I'm going to be dropping 3.0 since it doesn't have the stability that I want. So we (I) am going to move on to Chat.PY 4.0 and LSTC Green (since it should supposedly use less resources).
Offline
I swear I posted here when I was in school......
But I got disconnected somehow. That's probbably why.
Oh well, i'm posting now.
Magnie, are the chat.py broadcasts going to change? If they are, I would like to know what they will change into. For instance, "<chat2" could turn into "<chat4" or "<ch4t" (The second one was just a mash up of <chat and 4).
Offline
Magnie wrote:
laser314 wrote:
Is chat.py 3.0 working yet?
I'm going to be dropping 3.0 since it doesn't have the stability that I want. So we (I) am going to move on to Chat.PY 4.0 and LSTC Green (since it should supposedly use less resources).
But chat.py 2.0 doesn't work.
Offline