This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#276 2012-11-25 06:16:25

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#277 2012-11-25 06:58:58

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.


I'm back.
Maybe.

Offline

 

#278 2012-11-25 07:07:27

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

Does Scratch use TCP connection or UDP connection? Cause' I can make my client work with a UDP connection but not a TCP connection.


I'm back.
Maybe.

Offline

 

#279 2012-11-25 11:16:15

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.

Last edited by nXIII (2012-11-25 11:20:01)


nXIII

Offline

 

#280 2012-11-25 11:20:44

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.


I'm back.
Maybe.

Offline

 

#281 2012-11-25 11:25:50

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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!


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#282 2012-11-25 11:38:25

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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:

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(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.


I'm back.
Maybe.

Offline

 

#283 2012-11-25 14:05:50

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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 support the Free Software Foundation. Protect our digital rights!

Offline

 

#284 2012-11-25 15:04:20

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

DigiTechs wrote:

Does Scratch use TCP connection or UDP connection?

TCP.


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#285 2012-11-25 15:05:37

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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:

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

I'm back.
Maybe.

Offline

 

#286 2012-11-26 11:03:33

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.


I'm back.
Maybe.

Offline

 

#287 2012-11-26 11:12:21

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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)

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(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)


I'm back.
Maybe.

Offline

 

#288 2012-11-26 11:32:31

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.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  yikes
Kinda not fair. Well, I suppose everything's not fair to some extent.

Last edited by DigiTechs (2012-11-26 11:36:55)


I'm back.
Maybe.

Offline

 

#289 2012-11-26 11:52:26

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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?


I'm back.
Maybe.

Offline

 

#290 2012-11-26 12:06:36

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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)
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  yikes
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!


I'm back.
Maybe.

Offline

 

#291 2012-11-26 12:10:43

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.

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..""
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)


I'm back.
Maybe.

Offline

 

#292 2012-11-26 12:56:43

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

You want:

Code:

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

 

#293 2012-11-26 13:23:14

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

Magnie wrote:

You want:

Code:

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.

Ok then, I was just wondering because Lua had different string concatetions (e.e I can never spell that right, unless I just did  tongue ) than in Python.


I'm back.
Maybe.

Offline

 

#294 2012-11-27 16:52:06

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

No one's online! D:


I'm back.
Maybe.

Offline

 

#295 2012-12-04 13:51:05

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.


I'm back.
Maybe.

Offline

 

#296 2012-12-04 17:08:30

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.  tongue

Hopefully this thread will become much more active once I get LSTC Green out and I release Chat.PY 4.

Offline

 

#297 2012-12-04 20:55:24

laser314
Scratcher
Registered: 2010-07-16
Posts: 100+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

Is chat.py 3.0 working yet?

Last edited by laser314 (2012-12-04 20:55:56)


http://alpha.scratch.mit.edu/scratchr2/static//images/logo_sm.png 2.0 Alpha Tester!http://i49.tinypic.com/1zckcqb.png

Offline

 

#298 2012-12-04 21:54:18

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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

 

#299 2012-12-05 11:06:33

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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).


I'm back.
Maybe.

Offline

 

#300 2012-12-05 11:11:21

laser314
Scratcher
Registered: 2010-07-16
Posts: 100+

Re: Little Server That Can (Chat.PY, Bit Art, etc.)

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.


http://alpha.scratch.mit.edu/scratchr2/static//images/logo_sm.png 2.0 Alpha Tester!http://i49.tinypic.com/1zckcqb.png

Offline

 

Board footer