Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to stream real time data into the browser 2

Status
Not open for further replies.

MAWarGod

Programmer
Feb 15, 2002
352
US
I am trying to make a functional skeleton of a web based chat server with perl/cgi
Using a basic framework for this concept and functions. I haven’t been able to find any open source programs like this that showed the concept on how to stream real time data into the browser like the web based chat rooms on chatro, webmaze or alterrealm does
So I guess I need a login page (login.html) that passes, pushes or strings the data input or info that I would name $USER, $LOGIN, $PASSWORD, $USERPASS, $HISTORY, $NOPIC and $SAYS now this would also pass name:user ip to a file log userip.txt
<FORM METHOD="POST"
ACTION=" target=_top>
<TABLE WIDTH="550" BORDER="1" CELLSPACING="0">
<TR>
<TD ALIGN="RIGHT">Name:</TD>
<TD> <INPUT NAME="USER" VALUE="To Lazy to type" SIZE="20"></TD>
</TR>
<TR>
<TD ALIGN="CENTER" VALIGN="MIDDLE">
<TABLE WIDTH="100%" BORDER="0"
CELLSPACING="0">
<TR>
<TD ALIGN="RIGHT">Login:</TD>
<TD> <INPUT NAME="LOGIN"
SIZE="10"></TD>
</TR>
</TABLE> </TD>
<TD ALIGN="CENTER" VALIGN="MIDDLE">
<TABLE WIDTH="100%" BORDER="0"
CELLSPACING="0">
<TR>
<TD ALIGN="RIGHT">Password:</TD>
<TD> <INPUT NAME="USERPASS"
VALUE="" SIZE="10"></TD>
</TR>
</TABLE> </TD>
</TR>
<TR>
<TD ALIGN="CENTER" VALIGN="MIDDLE" COLSPAN="2">
<SELECT NAME="SAYS" SIZE="1">
<OPTION VALUE="<i>walks in....</i>">Visitors</OPTION>
<OPTION VALUE="<i>is within ....</i>">Member</OPTION>

</SELECT> </TD>
</TR>
<TR>
<TD ALIGN="CENTER" VALIGN="MIDDLE" COLSPAN="2">
<TABLE WIDTH="100%" BORDER="0"
CELLPADDING="03" CELLSPACING="0">
<TR>
<TD ALIGN="CENTER"
VALIGN="MIDDLE">History: <INPUT NAME="HISTORY" VALUE="5" SIZE="2" MAXLENGTH="2"></TD>
<TD ALIGN="CENTER" VALIGN="MIDDLE">Verbose <select name="VERBOSE" size="1"><option selected value="0">NO</option><option value="1">YES</option></select></TD>
<TD ALIGN="CENTER" VALIGN="MIDDLE"><b>Image Filtering <select
name="NOPIC" size="1">
<option selected value="0">Avatars On</option>
<option value="1">No Avatars</option>
<option value="1">No Images</option>
</select></TD>
</TR>
</TABLE> </TD>
</TR>
<TR>
<TD ALIGN="CENTER" VALIGN="MIDDLE"
COLSPAN="2"><INPUT TYPE="SUBMIT" VALUE="Chat Now"></TD>
</TR>
</TABLE> </FORM>

userip.txt
$user:$ip
myname:123.123.12.09
even if the file read from body log

room body log or body.txt
207.255.110.120:(Sun Sep 25 16:04:54 2005) To Lazy to type : <i>walks in....</i><BR>
or
207.255.110.120:(Sun Sep 25 16:05:05 2005) <img src= align=left><center></img><br><center><FONT FACE=garamond><FONT COLOR =silver><FONT SIZE=4>MY NAME<br>*some words*</font><br>*some more words*<br><FONT COLOR =silver><FONT SIZE=4><a target=_window>My site</a><br><a href= COLOR =blue><FONT SIZE=3>: <i>walks in....</i><BR>

the time and date would be nice but til I get basics

frames.pl a parser that validates the user login and enter post then parses them
also displaying banner.pl, body.txt and info.pl
<html>

<frameset rows="120,*">
<frame name=BANNER src="BANNER.pl?USER=$USER&SAYS=$SAYS$">
<frame name="BODY" src="BODY.txt?USER=$USER&SAYS=$SAYS$">
</frameset>
<frame name="INFO" src="info.pl or cgi or even html">
</frameset>

banner.pl serves parsed form and adds in current chatters name and list of other chatters a list of actions both read from a txt file or files on the server.

[MAIN]
<CENTERfont color=blueB> : $1 - $2 users</B/CENTER>
<PRE>
<B>$USER</BACTIONLISTUSERLIST>
Message: <INPUT TYPE="TEXT" NAME="SAYS" SIZE=40 VALUE=""
INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="* Submit *">
<INPUT NAME="LOGIN" TYPE="HIDDEN" VALUE="$LOGIN"
INPUT NAME="USER" TYPE="HIDDEN" VALUE="$USER"
INPUT NAME="USERPASS" TYPE="HIDDEN" VALUE="$USERPASS"
INPUT NAME="PASSWORD" TYPE="HIDDEN" VALUE="$PASSWORD"
INPUT NAME="NOPIC" TYPE="HIDDEN" VALUE="$NOPIC"
INPUT NAME="VERBOSE" TYPE="HIDDEN" VALUE="$VERBOSE"
INPUT NAME="HISTORY" TYPE="HIDDEN" VALUE="$HISTORY"
/FORMFORM ACTION="/" METHOD="POST" TARGET="_top"
INPUT NAME="LOGIN" TYPE="HIDDEN" VALUE="$LOGIN"
INPUT NAME="USER" TYPE="HIDDEN" VALUE="$USER"
INPUT NAME="USERPASS" TYPE="HIDDEN" VALUE="$USERPASS"
INPUT NAME="PASSWORD" TYPE="HIDDEN" VALUE="$PASSWORD"
INPUT NAME="NOPIC" TYPE="HIDDEN" VALUE="$NOPIC"
INPUT NAME="VERBOSE" TYPE="HIDDEN" VALUE="$VERBOSE"
INPUT NAME="HISTORY" TYPE="HIDDEN" VALUE="$HISTORY"
INPUT NAME="GOTO" TYPE="SUBMIT" VALUE="Goto"
GOTOLISTINPUT NAME="LEAVEIT" TYPE="SUBMIT" VALUE="Leave"
INPUT NAME="RELOAD" TYPE="SUBMIT" VALUE="Reload"
INPUT NAME="CONFIG" TYPE="SUBMIT" VALUE="Config">


And body is a html read out of the chat log or txt

$ VERBOSE is a line separating the posts
$ HISTORY is how many past posts the login $USER wants to see when entering room
USER and USERPASS are for members the room
LOGIN and $PASSWORD are from the administrators of the room
NOPIC turns picture display on and off on entering the room
Goto or leave redirects users to other room or url
CONFIG allows $USER to redo $USER name, pic’s on or off ,verbose

Now in most html chat the $user will input a string html code like

<img src= align=left><center></img><br><center><FONT FACE=garamond><FONT COLOR =silver><FONT SIZE=4>MY NAME<br>*some words*</font><br>*some more words*<br><FONT COLOR =silver><FONT SIZE=4><a target=_window>My site</a><br><a href= COLOR =blue><FONT SIZE=3>

help me I am going crazy lol






MA WarGod

I believe if someone can think it, it can be programmed
 
Most perl based chats probably use a meta refresh tag to update the chat box.

- Kevin, perl coder unexceptional!
 
I think I have a java script that will work

<SCRIPT LANGUAGE="JavaScript1.1">
<!--
var scrolling = false;
function toggleScrolling( )
{
if ( scrolling == true )
{
scrolling = false;
parent.BODY.clearTimeout();
parent.BODY.autoScrollOn = 0;
parent.BODY.scrollOff();
parent.BODY.onblur = parent.BODY.scrollOffFunction;
} else {
scrolling = true;
parent.BODY.autoScrollOn = 1;
parent.BODY.scrollOn();
parent.BODY.onblur = parent.BODY.scrollOnFunction;
parent.BODY.scroll(0, 65000);
parent.BODY.setTimeout('scrollWindow()', 200);
} // end if
} // end toggleScrolling
document.write('<INPUT NAME=AUTOSCROLL TYPE=CHECKBOX onclick="toggleScrolling()"');
if ( parent.BODY != null && parent.BODY.autoScrollOn != null )
{
if ( parent.BODY.autoScrollOn == 1 )
{
document.write(' CHECKED');
} // end if
scrolling = ( parent.BODY.autoScrollOn == 1 );
} else {
// assume it's on, since it starts on
if (0$AUTOSCROLL$ == 1)
{
document.write(' CHECKED');
scrolling = true;
}
}
document.write('> Auto Scrolling');
// -->
</SCRIPT>
</TD></TR> </TABLE>
<script> <!--
saysFocus(); // -->
</script>
</FORM></BODY></HTML>


MA WarGod

I believe if someone can think it, it can be programmed
 
its not the body I am worried about its the banner I have no clue how to from this to stream

MA WarGod

I believe if someone can think it, it can be programmed
 
javascript isn't my bag. Probably best to ask javascript questions in the javascript forum

- Kevin, perl coder unexceptional!
 
$USER, , $USERPASS, $LOGIN, $PASSWORD, $HISTORY, $NOPIC, $SAYS, $AUTOSCROLL, $ACTION, $ WHOTO

these all would be user input on login or index page dropdowns or txtbox or checkboxes

So how is the best way to from these to stream real time data into the browser.


USER=Myname the one I typed in txtbox
USERPASS=Mypass the one I typed in txtbox (if one uses will not have to have a pass to enter room)
SAYS=Enters+the+room... default or typed in text box
HISTORY=20 (how many lines up on the temp log) entered in txt box
VERBOSE=1 1 = <HR> 0 = nothing or on and off
LOGIN= ( the admin name)
PASSWORD (the admin password)
ACTION here would be hidden and set to nothing, but reads off a file of actions the chatter could have in room
WHOTO=ALL is set to all for entering the room, but in room the chatter could select who they want to talk at, like MYNAME SAYS to HERNAME Hello

So I want these to write to a file (or be logged)

USER:USERPASS:LOGIN:pASSWORD:SAYS:HISTORY:VERBOSE:ACTION: WHOTO

#/usr/bin/perl

use strict;
use warnings;
use CGI qw:)standard);


my $filename = 'path/to/file/log.txt';

&noFileData() unless(defined(param('fileData')));

&updateFile();

sub noFileData
{
open FILE, "<$filename" or die "Cannot open $filename: $!";
my @lines = <FILE>;
close FILE;

form(join("",@lines))
;
exit;
}

sub updateFile
{
my $data = param('fileData');

open FILE, ">$filename" or die "Cannot open $filename: $!";
print FILE $data;
close FILE;

form($data);
print "<BR><CENTER>File Updated at ".localtime()."</CENTER>\n";
}

sub form
{
my $data = shift;
print qq ~Content-type: text/html

<HTML>
<HEAD>
<TITLE> test </TITLE>


the input html goes here



but how do I multiple data without chunk?


MA WarGod

I believe if someone can think it, it can be programmed
 
What you want might be accomplished using whats called a non parsed header and server push. The CGI module supports both and can be read about in the CGI modules documentation.

Besides that I am not sure, maybe someone else will have some suggestions.

- Kevin, perl coder unexceptional!
 
Try non-buffered I/O.

Code:
#!/usr/bin/perl -w

$| = 1; # turn off output buffering

print "Content-Type: text/html\n\n";

print "Hello world";

for (0..10) {
   print ".";
   sleep 1;
}

print "I am a ";

foreach (qw(P e r l)) {
   print $_;
   sleep 2;
}

print " script. ";

foreach (qw(Nice to meet you)) {
   print "$_ ";
   sleep 1;
}

print "<p>";

print "Your environment variables are:<br>\n";

foreach my $key (keys %ENV) {
   print "$key is";
   for (1..3) {
      print ".";
      select (undef,undef,undef,0.10);
   }
   print "$ENV{$key}<br>\n";
}

print "<br>Okay, I'm done!";
exit;

Albeit this is an extreme example, but it shows that your Perl script can gradually output stuff. You could use a while(1) loop to get the latest chat message and output it.

Code:
while (1) {
   # read from the database or something
   if ($time_of_latest_msg > $time_of_last_msg) {
      print "[$sender] $msg<br>\n";
   }
}

This would depend on your server setup I think though, some servers have a timeout and would eventually kill your script if it wasn't finishing fast enough.

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Thank You Kirsle

one more question would I be better off to use a database

mysql or ect.

MA WarGod

I believe if someone can think it, it can be programmed
 

Kirsle

open(MSG,"<$htmlpath/msgfile.cgi");
while(<MSG>){
($line++)&&($line > 50)&&(last);
print MSGT $_;
}
close MSG;
close MSGT;

I sorta used that while as well

thank You again

MA WarGod

I believe if someone can think it, it can be programmed
 
'$.' is the input record line number:

Code:
open(MSG,"<$htmlpath/msgfile.cgi");
while(<MSG>){
  [b]last if $. > 50;[/b]
  print MSGT;
  }
close MSG;
close MSGT;

- Kevin, perl coder unexceptional!
 
ok one more thing


sub PageOut{
#format
local($file) = @_;
open(OUT,"$file");
while(<OUT>){
$_ =~ s/in\((.+?)\)/$in{$1}/g;
print;
}
close OUT;
}
1;

when entering chat I can use a = at all
and We need to as our html tags

<img src= align=left><center></img><br><center><FONT FACE=garamond><FONT COLOR =silver><FONT SIZE=4>MY NAME<br>*some words*</font><br>*some more words*<br><FONT COLOR =silver><FONT SIZE=4><a target=_window>My site</a><br><a href= COLOR =blue><FONT SIZE=3>





MA WarGod

I believe if someone can think it, it can be programmed
 
huh no edit button

I think this is where it is
# Split into key and value.
$loc = index($in[$i],"=");
$key = substr($in[$i],0,$loc);
$val = substr($in[$i],$loc+1);
$in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;
}
}
}

$loc = index($in[$i],"=");

is there a way around it? I need the =

MA WarGod

I believe if someone can think it, it can be programmed
 
I don't get why you're doing such complicated stuff just to separate the key from the value.

Code:
my $test = "message=hello world! <img src=[URL unfurl="true"]http://mysite.com/logo.gif[/URL] width=200 height=50 alt='my logo'>";

my ($key, $value) = split(/=/, $test, 2);
print "key: $key\nvalue: $value\n";

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top