socketdata and if?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
socketdata and if?
Continuing my playing around with sockets, I'm starting to pass data, or trying to, back and forth.
Here's what I can do: I can pass data from the client to a server, I can respond and send data back.
Now I'm trying to figure out how to respond to the data in LC.
Here's my code:
on newMessage theirSocket theMessage
put theMessage into field "fld"
if theMessage = "login_accepted" & return then
put "ACCEPTED" into field "statusField"
beep
end if
end newMessage
However, the if doesn't get triggered when theMessage is "login_accepted", even though that's what's being output to the fld field (For debugging)
It looks to me like there's a linebreak being added, which is odd since my server should only be sending "login_accepted" and nothing more, though that could have to do with sockets and signaling the end of the current stream, in any case I'm hoping that someone who knows what's going on could help me out.
I tried using if item 1 and a space for delimiter but that didn't work.
Here's what I can do: I can pass data from the client to a server, I can respond and send data back.
Now I'm trying to figure out how to respond to the data in LC.
Here's my code:
on newMessage theirSocket theMessage
put theMessage into field "fld"
if theMessage = "login_accepted" & return then
put "ACCEPTED" into field "statusField"
beep
end if
end newMessage
However, the if doesn't get triggered when theMessage is "login_accepted", even though that's what's being output to the fld field (For debugging)
It looks to me like there's a linebreak being added, which is odd since my server should only be sending "login_accepted" and nothing more, though that could have to do with sockets and signaling the end of the current stream, in any case I'm hoping that someone who knows what's going on could help me out.
I tried using if item 1 and a space for delimiter but that didn't work.
Marcus
Re: socketdata and if?
the number of chars in theMessage counts as 16, but the message is only 14 characters.
Marcus
Re: socketdata and if?
Ok, I've figured out a workaround for now, until I can figure out what's going on.
I'm using replaceText with regex to simply strip non-alphanumeric characters and comparing the resulting string.
I'm using replaceText with regex to simply strip non-alphanumeric characters and comparing the resulting string.
Marcus
-
- VIP Livecode Opensource Backer
- Posts: 4002
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: socketdata and if?
Hi trenatos,
sorry I can't help with sockets.
You could test for
or
and to find out what those extra characters are you could say
which gives you the ascii value of the last 2 characters
Kind regards
Bernd
sorry I can't help with sockets.
You could test for
Code: Select all
if theMessage contains "login_accepted" then
Code: Select all
if word 1 to - 1 of theMessage is "login_accepted" then
Code: Select all
put cr & charToNum(char - 2 of theMessage) && charToNum(char - 1 of theMessage) after field "statusField"
Kind regards
Bernd
Re: socketdata and if?
I considered using if contains, but it's going to be expanded to handle messaging later on, and if a user then places login_accepted (Or any of the other keywords) into a message, it will cause problems.
I'm going to implement a multi-stage process to make it sturdy as well as secure though, so I might end up using contains after all, but not until later on.
I'm going to implement a multi-stage process to make it sturdy as well as secure though, so I might end up using contains after all, but not until later on.
Marcus
-
- VIP Livecode Opensource Backer
- Posts: 327
- Joined: Sun Apr 15, 2012 1:17 am
- Location: USA
- Contact:
Re: socketdata and if?
Are you sending from a Windows computer? The extra 2 chars could be a lf+cr (line feed + carriage return)
Important! Sockets are always opened in binary mode. This means that LiveCode does not automatically convert between the other system's end-of-line convention and the line feed character (ASCII 10) that LiveCode uses internally to mark the end of a line. If you are reading or writing data one line at a time, be sure you know whether the other system uses line feed, return (ASCII 13), or both to mark the end of each line; if necessary, you will need to convert end-of-line markers yourself, after receiving or before sending the data. (The usual end-of-line marker on Mac OS and OS X systems is a return character; on Unix, a line feed; on Windows, a CRLF.)
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
Re: socketdata and if?
Yes, the testserver is a local server running on my dev-laptop, Windows 7.
I figured it was lf/cr, but not sure how to strip those characters through regex, or compare including them.
When finished it'll run on a linux server.
I figured it was lf/cr, but not sure how to strip those characters through regex, or compare including them.
When finished it'll run on a linux server.
Marcus
-
- VIP Livecode Opensource Backer
- Posts: 327
- Joined: Sun Apr 15, 2012 1:17 am
- Location: USA
- Contact:
Re: socketdata and if?
Try
if theMessage = ("login_accepted" & lf & cr) then
see if that works. The parentheses may be needed. Otherwise put that into a single variable and test it
if theMessage = ("login_accepted" & lf & cr) then
see if that works. The parentheses may be needed. Otherwise put that into a single variable and test it
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
Re: socketdata and if?
Ah but see if I strip the characters regardless, it won't matter what format the server sends (What platform it's running on)
Marcus
-
- VIP Livecode Opensource Backer
- Posts: 327
- Joined: Sun Apr 15, 2012 1:17 am
- Location: USA
- Contact:
Re: socketdata and if?
There you go. Good job
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
-
- VIP Livecode Opensource Backer
- Posts: 7237
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: socketdata and if?
A fast way to remove extra padding spaces, returns, linefeeds, and other non-text stuff is like this:trenatos wrote:Yes, the testserver is a local server running on my dev-laptop, Windows 7.
I figured it was lf/cr, but not sure how to strip those characters through regex, or compare including them.
put word 1 to -1 of theMessage into theMessage
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com