程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 聊天室實現私聊(一)

聊天室實現私聊(一)

編輯:ASP技巧

幾個要好的網友在聊天室裡閒聊的時侯,總會遇上有些話題是不想同一聊天室內別的聊友看見的,那麼聊天室支持悄悄話功能將是什分有必要的了,實現悄悄話的方法很多,現在小虎介紹一下利用session對象來實現它,用session對象來實現悄悄話,是有優缺點的,因為session對象的應用,其實就是客戶端cookies的使用。如果客戶端cookIEs不支持或關閉了,那麼程序運行起來就有問題了。那麼使用session對象就沒好處了嗎??非也,至少在編程上大大減少了工作量嘛,試想一下如果需要保存該用戶的聊天名字,那麼使用session對象就能直接保存他的名字了,如果沒有session對象。解決的方法看來就只有在處理發言的FORM裡設幾個隱含的textbox對象來保存,這樣處理起來會比教煩鎖,但好處是通用性廣,就算浏覽器不支持cookIEs仍然可用。

        說到這裡,大伙不妨試試網上眾多的ASP聊天室,試把浏覽器的安全選項(總是接受cookies關了)看看那個聊天室仍然正常地能使用。那個就問題百出來。。呵呵~~不要找小虎的聊天室來試哦....我的聊天室也需要cookIEs來支持的。不用session對象編程真是煩鎖很多。而且大部分浏覽器的安全配置的默認值是總是接受cookIEs。所以也不用太但心,只有少部分的用戶是比教特別的。

       現在從聊天室的發言開始,到處理發言,顯示發言,一步一步地讓大家了解一下是如何處理的,最後就加上悄悄話功能

  首先,我們的聊天室主要提供如下元素:發言人(你的名字),動作(叫嚷,抱抱...等),對象(這句話是向誰說的呢?),發言內容等四大元素。

  其中發言人(你的聊天名字)的名字是保存在發言FORM的user隱藏文本框中,這樣每當發言的時侯,在處理發言的says.ASP程序裡,就可以用request.form("user")來取得聊天名字。而動作則是由下拉框action來提供的,你可以自定義很多的動作類型,以滿足不同聊友的需要。而發言對象,也是由下拉框whoto來選定,這些名單是列出當前聊天室內所有用戶的名稱。如何判斷有新用戶進聊天室以及退出處理,這裡先不說了。因為構造一個完整的聊天室要做的工作還是挺多的。最後由一個says文本框提供發言內容。

  有了這幾個主要的聊天元素。我們就可以進行發言處理了。

  請看看下面的程序片段

 

'取得說話內容,並過濾其中的腳本語句
usersays=request.form("says")
if instr(usersays,"<script")>0 then
usersays=""
end if

'判斷是否有談話內容,沒有的話就不作處理

if len(usersays)<>0 then
session("whoto")=request.form("whoto")
session("action")=request.form("action")
if instr(usersays,chr(39))>0 then
usersays=replace(usersays,chr(39),"\"&chr(39))
end if
if instr(usersays,chr(34))>0 then
usersays=replace(usersays,chr(34),"\"&chr(34))
end if

application.lock

'定義一個交談緩沖區,後面將給大家仔細分析一下

dim chats(20)
for n=1 to 20
chats(n)=application("chats")(n)
next
username=request.Form("user")


usertime=Time
usertime="("&right(usertime,8)&")"
user_whoto=request.Form("whoto")
user_action=request.Form("action")
if application("counter")>19 then
application("counter")=0
end if
'不是耳語就加一
if request.form("action")<>"耳語" then
application("counter")=application("zj_1counter")+1
counter=application("zj_1counter")
'用戶的動作處理

select case request.FORM("action")
case "說話"
if request.FORM("whoto")="ALL" then
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> :<font color=#0000aa> "&usersays&"</font><BR><BR>"
else
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>與<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
end if
case "叫嚷"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>沖<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>大叫</I>:<B><font color=#0000aa> "&usersays&"</font></B><BR><BR>"
case "親親"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>親了親<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>的臉頰</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "擁抱"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>輕輕的抱著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font></I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "暴怒"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>朝<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>橫眉豎目道</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "輕推"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>輕輕地推了推<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>的胳膊</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "傻笑"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>傻兮兮的朝<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>笑笑</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "拳打"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>對准<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>就是一拳</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "腳踢"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>凌空一腿踢在<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>腦袋上</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "告戒"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>拉著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>手語氣心長地說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "耳光"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>狠狠地掴了<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>幾個耳光</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "鞠躬"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>畢恭畢敬地向<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>彎腰鞠躬</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "歉意"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>感到十二分的歉意,低聲對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "驚訝"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>驚訝的對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>吐了吐舌頭</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "哈欠"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>張大嘴巴,打了個哈欠</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "微笑"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>微微一笑</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "握手"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>緊緊的握住<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>的手</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "聳肩"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>對著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>聳聳雙肩</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "安慰"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>拍拍<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>的肩膀,雙眼關切的說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "生氣"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>氣鼓鼓的嘟嘴對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>道</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "大哭"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>忍不住對著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>放聲大哭</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "抽泣"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>雙手捂著臉,對著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>嗚嗚咽咽的</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "承諾"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>對著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>把胸脯拍得噼啪響</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "不捨"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>依依不捨地拉著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>的衣角</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "深情"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>用深情的眼神,癡癡地看著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font></I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "迷惑"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>抓抓頭皮看著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>露出迷惑的神情</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "不解"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>用不可思意的眼神看著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font></I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "幸災"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>幸災樂禍地對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "大笑"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>對著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>捧腹大笑</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "皺眉"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>皺著眉頭對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font></I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "委屈"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>很委屈地看著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>眼淚嘩啦啦地往下流</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "高興"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>興高采烈地對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "沮喪"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>滿臉沮喪地對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "失望"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>垂頭喪氣地對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "神經"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>神經兮兮地對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "神秘"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>非常神秘地對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>說</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "撒嬌"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>拉著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>手撒嬌</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "鬼臉"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>對著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>做了個鬼臉</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
case "無奈"
chats(counter)="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>對著<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>很無奈地</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
end select

'是耳語
else

'耳語處理
application("chat")="<font color=blue>"&usertime&"</font> <font color=black>"&username&"</font> <I>悄悄地對<font color=#00bb00>&nbsp;"&request.FORM("whoto")&" </font>耳語</I>:<font color=#0000aa> "&usersays&"</font><BR><BR>"
'對誰說
application("chatto")=request.FORM("whoto")
'說話的人
application("owner")=username
session("chat")="true"
end if


'談話全局化
application("chats")=chats


application.unlock


end if

  上面的程序是把用戶的發言和動作以及說話對象生成Html語句並保存起來。用以後面的在聊天室中顯示出來,其中請大家注意一下耳語是保存在application("chat")這個全局應用程序對象中的。好了,下節先為大家詳細分析一下說話的內容是如何處理。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved