Студопедия

Главная страница Случайная страница

КАТЕГОРИИ:

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Internet Chat Room




This example is a chat application that allows a collective group of users to send instant text messages back and forth across the Internet. The chat " rooms" are created by dynamically creating, reading, appending, and saving text files via FTP (to use the program, you'll need access to an available FTP server: FTP address, username, and password. Nothing else needs to be configured on the server).

! First, ask the user for the URL of an FTP server that will hold the text! of the chat room conversation: input " FTP URL", site$, " ftp.site.com" % default URL! Next, get the path name of the folder/directory that will hold the text! file containing the conversation: input " Folder", folder$, "./public_html/" % default folder! Next, get the name of the text file name that will hold the chat text: input " File", file$, " chat.txt" % default file! Next, get the FTP username: input " User Name", username$, " user"! Get the FTP password: input " Password", password$, " pass"! Get the user's screen name: input " Your Screen Name", name$, " Name"! Open the FTP connection: ftp.open site$, 21, username$, password$! Change directory to the necessary folder: ftp.cd folder$! This portion of code checks to see if the file$ text file already exists! on the server. First, set a flag variable to indicate that by default,! it's assumed the file doesn't exist: exists = 0! Now get a listing of files in the current FTP folder: ftp.dir files! Loop through the files in the folder list to see if any of the existing! file names match the file name given above. If a match is found, change! the " exists" flag variable to equal 1: list.size files, sfor i = 1 to s list.get files, i, page$ if file$ = page$ then exists = 1next i! If the file doesn't exist (i.e., the " exists" flag variable hasn't been! changed from 0 to 1), then create a new, blank text file and transfer! it to the server: if exists = 0 console.save " temp.txt" % create new blank text file ftp.put " temp.txt", file$ % transfer it to the server popup " New file created", 0, 0, 0endif! Now read the current contents of the online text chat, and save that! text to the file " chat.txt" on the local Android device: ftp.get file$, " chat.txt"! Next append some text to the downloaded text file above, indicating that! the user has entered the chat (" _Name_ has entered the room"). Opening! a text file with the " a" option APPENDS to the existing text in the file! (as opposed to erasing what's already there). Using concatenation, the! text written to the " chat.txt" file is the combined value of the current! date and time, the user's name, some static text, and a carriage return.! Notice the use of the underscore character to continue the concatenation! onto a second line: text.open a, chattext, " chat.txt" time y$, m$, d$, h$, n$, s$text.writeln chattext, m$ + " -" + d$ + ", " + h$ + ": " + m$ + ": " + ~ name$ + " has entered the room." text.close chattext! Now the program uses a forever-repeating Do/While loop to continually! wait for user input, and to do appropriate things with that input. The! following " flag$" variable is set to " continue". This variable will be! used later to determine if the user wants to end the program: flag$ = " continue" do! Clear the screen: cls! Display a greeting and some instructions. Notice the use of " \n"! to print a carriage return (new line), and the underscore symbol! (_) to continue concatenation onto numerous lines: print " ------------------------------------\n" + ~ " You are logged in as: " + name$ + " \n" + ~ " Type 'room' to switch chat rooms.\n" + ~ " Type 'quit' to end your chat.\n" + ~ " Enter blank text (just click OK)\n" + ~ " to periodically update the display.\n" + ~ " ------------------------------------"! Read the updated messages that are currently in the " chat.txt" text! file on the FTP server, assign the variable word currentChat$ to! that text, and print the contents: ftp.get file$, " chat.txt" grabfile currentChat$, " chat.txt" print " Here's the current chat text at: " + file$ print currentChat$! Pause a few seconds to allow the user to read the chat: pause 5000! Get some text to be added to the chat room, entered by the user.! Save the entered text in the string variable " enteredText$": input " You say: ", enteredText$! The If/Elseif/Else structure below is used to check for commands in! the text entered by the user. If the user enters " quit", empty! text, or some other text to be added to the conversation,! appropriate actions occur: if enteredText$ = " quit"! If the user typed " quit", stop the forever loop (which will exit! the program). You could alternately use the " d_u.break"! function here: flag$ = " quit" elseif enteredText$ < > " "! If the user entered some text (anything other than blank text),! Concatenate the text with the user's name and the static text! " says: ". Store that concatenated text in the string variable! " sentMessage$": sentMessage$ = name$ + " says: " + enteredText$! Now download the current chat text again (to make sure you're! working with the most recent update(s)), append the message! above to it, and then upload the appended text back to the FTP! server: ftp.get file$, " chat.txt" text.open a, chattext, " chat.txt" % remember, open to APPEND text.writeln chattext, sentMessage$ text.close chattext ftp.put " chat.txt", file$ endif! The only other possible option at this point is that the user! entered nothing (blank text, " "). In that case, the loop is simply! repeated, so the current chat text is again downloaded, the display! is updated, user input requested, the If/Then/Else conditions! evaluated again, etc. This goes on until the user types " quit". until flag$ = " quit"! When the forever repeating loop is exited, append a final message to the! chat text, close the FTP connectin, and end the program: cls print " Goodbye! " ftp.get file$, " chat.txt" text.open a, chattext, " chat.txt" % remember, open to APPENDtext.writeln chattext, h$ + ": " + m$ + ": " + name$ + ~ " has left the room." text.close chattextftp.put " chat.txt", file$ftp.closepause 1000end

The forever repeating loop structure used in this program is a very common outline for apps that repeatedly accept input from users, process the incoming data, and display resulting output data. It's especially useful in games that continually move lists of graphic items around a screen (more on that later in this tutorial).

Here's the entire program, without comments:

input " FTP URL", site$, " ftp.site.com" input " Folder", folder$, "./public_html/" input " File", file$, " chat.txt" input " User Name", username$, " user" input " Password", password$, " pass" input " Your Screen Name", name$, " Name" ftp.open site$, 21, username$, password$ftp.cd folder$exists = 0ftp.dir fileslist.size files, sfor i = 1 to s list.get files, i, page$ if file$ = page$ then exists = 1next iif exists = 0 console.save " temp.txt" ftp.put " temp.txt", file$ popup " New file created", 0, 0, 0endifftp.get file$, " chat.txt" text.open a, chattext, " chat.txt" time y$, m$, d$, h$, n$, s$text.writeln chattext, m$ + " -" + d$ + ", " + h$ + ": " + m$ + ": " + ~ name$ + " has entered the room." text.close chattextflag$ = " continue" do cls print " ------------------------------------\n" + ~ " You are logged in as: " + name$ + " \n" + ~ " Type 'room' to switch chat rooms.\n" + ~ " Type 'quit' to end your chat.\n" + ~ " Enter blank text (just click OK)\n" + ~ " to periodically update the display.\n" + ~ " ------------------------------------" ftp.get file$, " chat.txt" grabfile currentChat$, " chat.txt" print " Here's the current chat text at: " + file$ print currentChat$ pause 5000 input " You say: ", enteredText$ if enteredText$ = " quit" flag$ = " quit" elseif enteredText$ < > " " sentMessage$ = name$ + " says: " + enteredText$ ftp.get file$, " chat.txt" text.open a, chattext, " chat.txt" % remember, open to APPEND text.writeln chattext, sentMessage$ text.close chattext ftp.put " chat.txt", file$ endifuntil flag$ = " quit" cls print " Goodbye! " ftp.get file$, " chat.txt" text.open a, chattext, " chat.txt" % remember, open to APPENDtext.writeln chattext, h$ + ": " + m$ + ": " + name$ + ~ " has left the room." text.close chattextftp.put " chat.txt", file$ftp.closepause 1000end

Данная страница нарушает авторские права?


mylektsii.su - Мои Лекции - 2015-2024 год. (0.006 сек.)Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав Пожаловаться на материал