I was thinking more like this:
local inputs = {
"Hello world\nThis",
" is a broken message",
"\nI want to ",
"reassemble it!\n\n",
"end\n"
}
local function parseLine(emit)
local buffer = ""
return function (chunk)
local start = 1
for i = 1, #chunk do
local c = chunk:sub(i, i)
p{buffer=buffer,i=i,c=c}
if c == "\n" then
emit(buffer .. chunk:sub(start, i - 1))
buffer = ""
start = i + 1
end
end
buffer = buffer .. chunk:sub(start)
end
end
local handle = parseLine(p)
for i, chunk in ipairs(inputs) do
handle(chunk)
end
https://gist.github.com/creationix/8300163
On Mon, Jan 6, 2014 at 9:42 PM, develephant <[email protected]>wrote:
> Thank Tim, I really appreciate you taking the time to reply. I have to
> tell you I have been eating and sleeping Luvit for the last month. As a
> Lua junkie, I really have enjoyed learning about it and using it, so thank
> you.
>
> Anyway, I found this bit of code for node. I just need to rework it for
> Luvit. Does this look about right in theory?
>
> // define your terminator for easy reference, changesvar msgTerminator =
> '\n'// create a place to accumulate your messages even if they come in
> piecesvar buf;
>
> socket.on('data', function(data){
> // add new data to your buffer
> buf += data;
>
> // see if there is one or more complete messages
> if (buf.indexOf(msgTerminator) >= 0) {
> // slice up the buffer into messages
> var msgs = data.split(msgTerminator);
>
> for (var i = 0; i < msgs.length - 2; ++i) {
> // walk through each message in order
> var msg = msgs[i];
>
> // pick off the current message
> console.log('Data in server, sending to handle()');
> // send only the current message to your handler
> worker.handle(msg, socket);
> }
>
> buf = msgs[msgs.length - 1]; // put back any partial message into
> your buffer
> }});
>
> Thanks again.
>
>
> On Monday, January 6, 2014 3:21:41 PM UTC-6, Tim Caswell wrote:
>
>> There is no option built-in. I don't know of any existing libraries that
>> do this. I would write it as a parser function that wraps the callback.
>>
>> local function parseLine(emit)
>> -- setup local variables here
>> return function (chunk)
>> -- look for newlines in chunk flushing and emit()ing data as you come
>> across them
>> -- store leftover bytes in local variable for the next chunk.
>> end
>> end
>>
>> client:on("data", parseLine(function( line )
>> -- data is now line parsed.
>> end))
>>
>>
>>
>>
>> On Mon, Jan 6, 2014 at 2:14 PM, develephant <[email protected]>wrote:
>>
>>> Hello,
>>>
>>> I'm looking to see if there is a simple way to read the luvit tcp
>>> (smart) socket by line similar to the straight luasocket:
>>>
>>> client:*receive(*[pattern [, prefix]]*)*
>>>
>>> - '*l': reads a line of text from the socket. The line is terminated
>>> by a LF character (ASCII 10), optionally preceded by a CR character
>>> (ASCII 13). The CR and LF characters are not included in the returned
>>> line.
>>> In fact, *all* CR characters are ignored by the pattern. This is the
>>> default pattern;
>>>
>>>
>>> http://w3.impa.br/~diego/software/luasocket/tcp.html#receive
>>>
>>> In luvit for example:
>>> client:on( "data", function( data )
>>> --data is a line terminated by "\r\n"
>>> end)
>>>
>>> I'm a newbie on node, but I can't seem to find any option. Is this
>>> possible? Or will I need to parse it up myself? Any advice would be
>>> appreciated.
>>>
>>> Cheers.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "luvit" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "luvit" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
--
You received this message because you are subscribed to the Google Groups
"luvit" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.