使用dwr20實(shí)現(xiàn)服務(wù)器推客戶端_第1頁(yè)
使用dwr20實(shí)現(xiàn)服務(wù)器推客戶端_第2頁(yè)
使用dwr20實(shí)現(xiàn)服務(wù)器推客戶端_第3頁(yè)
使用dwr20實(shí)現(xiàn)服務(wù)器推客戶端_第4頁(yè)
使用dwr20實(shí)現(xiàn)服務(wù)器推客戶端_第5頁(yè)
已閱讀5頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、使用dwr2.0實(shí)現(xiàn)服務(wù)器推客戶端(結(jié)合響應(yīng)socket服務(wù)器推消息)1、在 web.xml中配置加入 dwr2.0 的推功能Set Character Encodingcom.leopard.filters.SetCharacterEncodingFilterencodingUTF-8struts-cleanuporg.apache.struts2.dispatcher.ActionContextCleanUp sitemeshcom.opensymphony.module.sitemesh.filter.PageFilter struts2org.apache.struts2.dispat

2、cher.FilterDispatcherSessionInactivityFilterjavawebparts.filter.SessionInactivityFilterforwardTo/errors.jsp 120 SessionInactivityFilter/*Set Character Encoding/*struts-cleanup/*sitemesh/*struts2/*contextConfigLocation/WEB-INF/classes/com/leopard/spring/applicationContext_*.xmlorg.springframework.web

3、.context.ContextLoaderListener!- freemarker 的servletfreemarkercom.opensymphony.webwork.views.freemarker.FreemarkerSe rvlet-*dwr-invokerorg.directwebremoting.servlet.DwrServlet調(diào)試DWR,發(fā)布系統(tǒng)時(shí)應(yīng)將其設(shè)為falsedebugtrue使用服務(wù)器推技術(shù)(反轉(zhuǎn)AJAX) activeReverseAjaxEnabledtrueinitApplicationScopeCreatorsAtStartuptruemaxWaitAf

4、terWrite1004dwr-invoker/dwr/*imgorg.leopard.web.util.AuthImgimg/authImg!-freemarker*.ftldwr/dwr/*Admin/admin-/login/login.jsp這里與dwr1.0的配置是不一樣的dwruk.ltd.getahead.dwr.DWRServletdebugtrue2、dwr的服務(wù)器端執(zhí)行方法代碼package com.leopard.dwr;import java.io.BufferedReader;import java.io.IOException;import java.io.Inpu

5、tStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import .Socket;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;impor

6、t javax.servlet. HYPERLINK http:/http.HttpServletRequest http.HttpServletRequest;import org.directwebremoting.ScriptBuffer;import org.directwebremoting.ScriptSession;import org.directwebremoting.ServerContext;import org.directwebremoting.ServerContextFactory;import org.directwebremoting.WebContextFa

7、ctory;import xy.dwr.Util;import com.leopard.web.util.SingletonSocket;/*處理聊天相關(guān)* author leopard*/public class ChatManager (/*保存當(dāng)前在線用戶列表*/public static List users = new ArrayList();/* 保存當(dāng)前的scriptsession*/public static Map scriptmap = new HashMap();/*保存當(dāng)前的user和session對(duì)應(yīng)*/public static Map userScriptMap

8、= new HashMap();/*更新在線用戶列表*1 param username待添加到列表的用戶名*1 param flag是添加用戶到列表,還是只獲得當(dāng)前列表*1param request*1 return 用戶userid*/pUblic String updateUsersList(String username, boolean flag, HttpServletRequest request) (System. out .println(進(jìn)入);User user = null;if (flag) (/這里取會(huì)話(HttpSession)的id為用戶iduser = new

9、User(request.getSession().getId(), username);/保存用戶到列表users.add(user);/將用戶id和頁(yè)面腳本session綁定this.setScriptSessionFlag(user.getUserid();/將用戶和頁(yè)面sessio綁定this.setUserScriptSession(user);Util util = new Util(this.leopardScriptSessions(request);/處理這些頁(yè)面中的一些元素util.removeAllOptions(users);util.addOptions(users,

10、 users, username);util.removeAllOptions(receiver);util.addOptions(receiver, users,userid,username);if(!flag)(return null;return user.getUserid();/*獲得當(dāng)前在線的scriptsession列表(解決了刷新改變scriptsession的問(wèn)題)*1param request*1return*/public CollectionleopardScriptSessions(HttpServletRequest request)(/獲得 scriptSess

11、ion,把 httpsession 和 scriptSession 存至 到map 中ScriptSession scriptSession =WebContextFactory.get().getScriptSession();scriptmap.put(request.getSession().getId(), scriptSession);Collection sessions = new HashSet();sessions.addAll(scriptmap.values();return sessions;/*綁定用戶名和scriptsession*1param user*/publ

12、ic void setUserScriptSession(User user)(userScriptMap.put(user.getUsername(),WebContextFactory.get().getS criptSession();/*將用戶id和頁(yè)面腳本session綁定*1param userid*/public void setScriptSessionFlag(String userid) (WebContextFactory.get().getScriptSession().setAttribute(userid, userid);/*根據(jù)用戶id獲得指定用戶的頁(yè)面腳本se

13、ssion*1param useridrequest*/SuppressWarnings(unchecked)public ScriptSession getScriptSession(String userid, HttpServletRequest request) (ScriptSession scriptSessions = null;Collection sessions = new HashSet();sessions.addAll(ServerContextFactory.get(request.getSession().get ServletContext().getScrip

14、tSessionsByPage(/leopardweb/main/main.jsp); for (ScriptSession session : sessions) ( String xuserid = (String) session.getAttribute(userid); if (xuserid != null & xuserid.equals(userid) ( scriptSessions = session;return scriptSessions;public void cleanSession(String userid , HttpServletRequest reque

15、st) (users.remove(0);scriptrnap.remove(userid);/*發(fā)送消息*Bparam sender 發(fā)送者*1 param receiverid 接收者id*1 param msg消息內(nèi)容*1param request*/public void send(String sender,String receiverid,String msg,HttpServletRequest request)(ScriptSession session = this.getScriptSession(receiverid, request);ScriptBuffer scr

16、ipt = new ScriptBuffer();script.appendScript(tempfuc().appendData(nihao!).appendScript (););session.addScript(script);Util util = new Util(session);util.setStyle(showMessage, display, );util.setValue(sender, sender);util.setValue(msg, msg);SuppressWarnings (static-access)public void socket() (Single

17、tonSocket.getInstance();這里的socket連接采用了單例模式,保證整個(gè)應(yīng)用中只有一個(gè)連接package com.leopard.web.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import .Socket;import java.util.Collection;import org.directwebremoting.ScriptBuffer;import org.di

18、rectwebremoting.ScriptSession;import com.leopard.dwr.ChatManager;/*單例模式的socket的連接方式* author leopard*/public class SingletonSocket (private SingletonSocket() (try (Socket so = new Socket(localhost, 8500);System. out .println(連接服務(wù)器。);InputStream inputStream = so.getInputStream();BufferedReader input =

19、 new BufferedReader(newInputStreamReader(inputStream);/* socket服務(wù)器端推數(shù)據(jù)到socket客戶端*/while (true) (/input.readLine()不能放到判斷里面,要不會(huì)死鎖,最好是在最外面 傳值給變量String socketData = input.readLine(); /System.out.println(ChatManager.userScriptMap.size(); ScriptSession session =ChatManager.userScriptMap.get(leopard); if (

20、session != null ) (ScriptBuffer script = new ScriptBuffer(); script.appendScript(tempfuc() .appendData(socketData).appendScript();); session.addScript(script);/*Collection col = ChatManager.scriptmap.values();System.out.println(歹0表長(zhǎng)度: + col.size(); for (ScriptSession session : col) (ScriptBuffer scr

21、ipt = new ScriptBuffer(); script.appendScript(tempfuc().appendData(input.readLine().appendScript();); session.addScript(script);*/ catch (IOException e) ( e.printStackTrace();private static SingletonSocket socketinstance = null;synchronized public static SingletonSocket getInstance() ( if (socketins

22、tance = null) (socketinstance = new SingletonSocket(); return socketinstance;3、在dwr。Xml文件中配置 convert converter=bean match=com.leopard.dwr.User/ 4、實(shí)現(xiàn)socket的服務(wù)器推客戶端/ socket服務(wù)器端代碼/實(shí)現(xiàn)了定時(shí)往socke客戶端發(fā)送消息/ 一個(gè)線程檢查socket客戶端的用戶連接情況/ 一個(gè)線程往socket客戶端發(fā)送消息package com.leopard.socket;import java.io.*;import .*;import

23、java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Timer;import java.util.TimerTask;public class EchoServer (static List sockets = new ArrayList ();public EchoServer() (Socket incoming; /客戶端的鏈接; ServerSocket so; /服務(wù)器 socket try (so = new ServerSocket(8500);System. out .

24、println(等待客戶端連接。);while (true) ( try (incoming = so.accept();sockets.add(incoming);System. out .println( 已連接客戶端。);GetInfo gi = new GetInfo(incoming);/ 調(diào)用線程ClientClose client = new ClientClose(); /調(diào)用檢查socket 連接的線程 catch (IOException e) ( e.printStackTrace(); catch (IOException e) ( e.printStackTrace(

25、); public static void main(String args) ( new EchoServer();/*從客戶端得到信息* author Administrator*/private static class GetInfo implements Runnable ( / 線程類Date now = new Date(System.currentTimeMillis();private Socket incoming; /客戶端的鏈接;private String s_formClient = null; /從客戶端傳過(guò)來(lái)的數(shù)據(jù)private BufferedReader b

26、uffer_formClient; /從客戶端傳過(guò)來(lái)的數(shù)據(jù)流; Thread thread; PrintWriter out;Timer m_MyTimer=new Timer();public GetInfo(Socket incoming) ( try ( this.incoming = incoming; out = newPrintWriter(incoming.getOutputStream(), true); /必須要有 true,才會(huì)返回到客 戶端/*從客戶端來(lái)的數(shù)據(jù)buffer_formClient = new BufferedReader(newInputStreamRead

27、er(incoming.getInputStream();thread = new Thread(this); thread.start(); catch (IOException e) ( e.printStackTrace(); public void run() (m_MyTimer.schedule(new TimerTask() (public void run() (for (int i=0;i sockets .size();i+) /利用 For 循環(huán)遍歷集合Socket s2 = (Socsockets .get(i);/創(chuàng)建Socket對(duì) 象S2,強(qiáng)轉(zhuǎn)成Socket,并獲取

28、下標(biāo) PrintWriter out;try System.out. println(進(jìn)入發(fā)消息);out = new PrintWriter(s2.getOutputStream();out.println(測(cè)試。);/打印字符out.flus /刷新該流的緩沖 catch (IOException e) e.printStackTrace();/文本流輸出new Date(System.currentTimeMillis () + 1000),10000); private static class ClientClose implements RunnableDate now = new Date(System.currentTimeMillis();Thread thread;PrintWriter out;Timer m_MyTimer=new Timer();pUblic ClientClose() thread = new Thread(this);thread.start();public void run() (m_MyTimer.schedu

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論