CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Websockets. need help

 



sriubytis
Novice


Feb 27, 2014, 1:49 PM

Post #1 of 9 (2512 views)
Websockets. need help Can't Post

Hello there, i would like to get any help about OOP / websockets in perl.

heres the code:

Code
############################# 
my @hash2conn={};

$cc=Net::WebSocket::Server->new(
listen => 8080,
on_connect => sub {
my ($serv, $conn) = @_;
my $hash= substr $conn , 40,9;
}

$conn->on(
utf8 => sub {
my ($conn, $msg) = @_;

print $msg "\n";
$conn->send_utf8($msg);

},

);

},
);
$cc->start;
#############################


my question:

how can i access - $conn->send_utf8($msg);
from outside package?

my point is to sent messages from other thread using @hash2conn as user array.

sorry for the mess, but please if anyone could help..
i started with perl just today, and i need some part to be finished until next week, HELP! :(
--------------------------------
Just another odd developer
4y php; perl since 2014/02/27


(This post was edited by FishMonger on Mar 1, 2014, 6:04 AM)


Laurent_R
Veteran / Moderator

Feb 28, 2014, 10:32 AM

Post #2 of 9 (2217 views)
Re: [sriubytis] Websockets. need help [In reply to] Can't Post

Please use code tags to make the code readable:


Code
my @hash2conn={}; 

$cc=Net::WebSocket::Server->new(
listen => 8080,
on_connect => sub {
my ($serv, $conn) = @_;
my $hash= substr $conn , 40,9;
}

$conn->on(
utf8 => sub {
my ($conn, $msg) = @_;

print $msg "\n";
$conn->send_utf8($msg);

},

);

},
);
$cc->start;


As for your question, '$conn->send_utf8($msg);' is not a piece of data but an action. Accessing this does not seem to make much sense.


sriubytis
Novice


Mar 1, 2014, 4:50 AM

Post #3 of 9 (2162 views)
Re: [Laurent_R] Websockets. need help [In reply to] Can't Post

Oh really. I didnt knew its a method, who would :o lol. Srsly, i wrote everyting; the point is to send messages via other thread to all users. Let it be from other package or whatever.

If you can not provide any solution related answer, please do not reply at all.


Sly

--------------------------------------------------
sorry about tags. seems \t were cut
--------------------------------------------------
thread 1. core (server)
receives data and pushes to thread 2
thread 2 processing (doesnt touch my question)
processes data and splits output for thread 3
thread 3 sends colected data to users (my need)

thread 3 iterates user list from output array, similar to @hash2conn , identifies connections with @hash2conn itself (where should be relation to the user object, in my stupid opinion) , and sends packed binary package

point clear?
--------------------------------
Just another odd developer
4y php; perl since 2014/02/27


(This post was edited by sriubytis on Mar 1, 2014, 5:05 AM)


FishMonger
Veteran / Moderator

Mar 1, 2014, 6:47 AM

Post #4 of 9 (1991 views)
Re: [sriubytis] Websockets. need help [In reply to] Can't Post

I have not used that module so I can't provide a working example on its proper usage, but I do see that you have an extra brace which will cause a problem. Specifically, it will generate this warning:

Quote
Scalar found where operator expected near "$conn"


Your indentation is also inconsistent and should be cleaned up.


Code
my $cc = Net::WebSocket::Server->new( 
listen => 8080,
on_connect => sub {
my ( $serv, $conn ) = @_;
my $hash = substr $conn, 40, 9;

$conn->on(
utf8 => sub {
my ( $conn, $msg ) = @_;

print $msg "\n";
$conn->send_utf8($msg);
},
);
}
);



Laurent_R
Veteran / Moderator

Mar 1, 2014, 8:14 AM

Post #5 of 9 (1988 views)
Re: [sriubytis] Websockets. need help [In reply to] Can't Post


In Reply To
Srsly, i wrote everyting


Really? Well, if you wrote everything, then I would assume that you should be able to use it and don't need help. Also, it must be a very strange coincidence that the sample code provided in the first example of the documentation for the Net::WebSocket::Server module (http://search.cpan.org/~topaz/Net-WebSocket-Server-0.001003/lib/Net/WebSocket/Server.pm is almost exactly identical to the code you presented above:

Code
    use Net::WebSocket::Server; 

Net::WebSocket::Server->new(
listen => 8080,
on_connect => sub {
my ($serv, $conn) = @_;
$conn->on(
utf8 => sub {
my ($conn, $msg) = @_;
$conn->send_utf8($msg);
},
);
},
)->start;

except, of course, that it is better indented and that it does compile. Presumably, when the author of that module copied your code to make his example, he took the time to correct your bugs and tidy up indentation. Tongue


In Reply To
If you can not provide any solution related answer, please do not reply at all.


Don't worry about that, if you take that tone, I'll certainly make no further effort to try to help you.


sriubytis
Novice


Mar 1, 2014, 9:02 AM

Post #6 of 9 (1983 views)
Re: [Laurent_R] Websockets. need help [In reply to] Can't Post

i do not see any point of printing here all the source. the question is about just that one part.

and indeed i started from example. same in php , same in python. and i chose perl as it was giving the best results

im not here to argue or prove anything. i am here because i need help in oop of perl.

DONT MAKE THIS WEIRD

-------------------------------
and FishMonger; whats the difference what module is it. it is a question of oop (well in perl its not different on every module lol, is it :D )
--------------------------------
Just another odd developer
4y php; perl since 2014/02/27


(This post was edited by sriubytis on Mar 1, 2014, 9:50 AM)


sriubytis
Novice


Mar 1, 2014, 9:10 AM

Post #7 of 9 (1981 views)
Re: [sriubytis] Websockets. need help [In reply to] Can't Post

guess its time to close the topic.

solution - compile php (as its just 1 reference and one static call to make it work) to c.
--------------------------------
Just another odd developer
4y php; perl since 2014/02/27


FishMonger
Veteran / Moderator

Mar 1, 2014, 12:38 PM

Post #8 of 9 (1966 views)
Re: [sriubytis] Websockets. need help [In reply to] Can't Post


Quote
and FishMonger; whats the difference what module is it. it is a question of oop (well in perl its not different on every module lol, is it :D )


That's a silly rhetorical question.

You didn't ask a generic question about OO programming in Perl. You asked about how to access/use a very specific method in a very specific OO module and the sample code you provided was incomplete and had syntax errors which would probably generate compilation errors.

Besides the primary documentation for Net::WebSocket::Server, which you already have seen but may not have read, you should read the documentation for Net::WebSocket::Server::Connection

http://search.cpan.org/~topaz/Net-WebSocket-Server-0.002003/lib/Net/WebSocket/Server/Connection.pm


sriubytis
Novice


Mar 1, 2014, 6:13 PM

Post #9 of 9 (1948 views)
Re: [FishMonger] Websockets. need help [In reply to] Can't Post

from php you can simply access a public property like users by $objName->prop / functions ..->funct(args).

if its complicated, you apply static access or use ref's

whatever i did, i could not make it work with perl :(

------------------------------------------------------------------
im done with perl for now, since i just havent time to do both study and rewriting. but.

i am still interested in this akward thing

---------------------------

Code
 

Net::WebSocket::Server

### a simple obj from package ###

->new(

### args for constructor ###

listen => 8080,
on_connect => sub {

### method from constructor O.o ###
my ($serv, $conn) = @_;
$conn->on(
utf8 => sub {
### method from arg-object o.O ###

my ($conn, $msg) = @_;
$conn->send_utf8($msg);
### method from arg-objects method O.o ###
},
);
},
)->start;


perhaps it could be called "specific" , or to speak as think - bad practice. but its not for me to tell lol.

so is there a way to put $conn as reference to be accessed from outside scope with its function, or the only solution is to rewrite package and add that method to the Net::WebSocket::Server objs sub?
--------------------------------
Just another odd developer
4y php; perl since 2014/02/27


(This post was edited by sriubytis on Mar 3, 2014, 4:13 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives