📄 index2a_chatpage3.htm
字号:
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/photoshop/index.html">Photoshop</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/flash/page1.html">Flash</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/asp/index.html">ASP</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/php/index.html">PHP</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/java/index.htm">Java</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/vb/index.htm">VB</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/c/index.htm">C、C++</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-backend/database/php_mysql/index.html">PHP/MySQL</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"><a href="../../1-backend/cgi_perl/perl_beginner/index.html">-Perl</a>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">-<a href="../../1-teach/other/index.htm">其它</a> </td>
</tr>
<tr>
<td colspan="2" bgcolor="#666699">
<div align="center"><font color="#FFFFFF">更多教程</font></div>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="17"><a href="../../1hdml/index.html">-HDML</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="23"><font face="宋体"><a href="../../1-backend/database/course/day1_1.html">-网络数据库</a></font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="14"><a href="../../1-backend/protocols/ping/index.html"><font face="arial, helvetica, sans-serif">-ping</font></a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="20"><a href="../../1-backend/cgi_perl/search_engine/index.html">-创建搜索引擎</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" height="16">-<a href="../../1adobe/GoLive/index.html">Adobe GoLive</a></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"><a href="../../1-backend/cgi_perl/templates/index.html">-模板</a></td>
</tr>
<tr bgcolor="#666699">
<td colspan="2" align="center"><font color="#FFFFFF">合作伙伴</font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" align="left">-<a href="http://www.5dmedia.com/" target="_blank">5D精英网</a></td>
</tr>
<tr align="center" bgcolor="#FFFFFF">
<td colspan="2"> <img src="../../Library/front_monkey.gif" width="59" height="68"></td>
</tr>
</tbody>
</table>
<!-- #EndLibraryItem --> </td>
<!-- End of headlines (column 1: left column) --> <!-- Gutter between columns 1 and 2 -->
<td width="10" height="794"><img src="http://www.sohu.com/images/pixel.gif" width=10></td>
<!-- Search box and directories (columns 2 and 3: middle columns, combined into one) -->
<td align=center valign=top width="558">
<div align="left"><!-- #BeginEditable "1" -->
<div align="center">
<p><font color="#FF0000"><strong><big><big>第3页:客户</big></big></strong></font><font
face="宋体"><br>
<!-- BYLINE --> 作者<font size="2" color="#000000"><a href="mailto:moses@wired.com"
title="Send
mail to Moses">Moses</a></font> </font></p>
<p align="left"><b>你所建立的客户端<font face="宋体">Java</font>小程序应该能同服务器和其他客户进行交谈。</b><font
face="宋体"> </font></p>
<p align="left">客户端是一个<font face="宋体">Java</font>小程序。调用服务器上的方法非常简单。要获得对服务器上的引用,客户端只需:
<p align="left"><font size="3"><font face="宋体">ChatServer server =
(ChatServer) java.rmi.Naming.lookup("rmi://serverhost.wired.com/ChatServer");
server.login("Joe", this); <br>
</font></font><font size="3"><font face="宋体">server.chat("Joe",
"I like Gorgonzola"); </font></font>
<p align="left"><font face="宋体"><tt>ava.rmi.Naming</tt></font>是连接到<font face="宋体">RMI
registry</font>的一个<font face="宋体">Java</font>类,从其可以获得指向闲聊服务器(<font
face="宋体">ChatServer</font>)的指针,指针基于上述<font face="宋体">URL</font>地址。标准的用法是省略<font
face="宋体">"<tt>rmi:</tt>"</font>,因为这是<font face="宋体"><tt>java.rmi.Naming</tt></font>所能理解的唯一通讯协议。<font
face="宋体"> </font>
<p align="center"><font face="宋体"><img
src="http://static.wired.com/webmonkey/code/97/28/stuff/registry.gif">
<br>
</font>客户登录到服务器</p>
<p align="left"><b>但是<font face="宋体">:</font></b> 闲聊是双向的交流。你必须将闲聊信息发送给服务器,闲聊服务器也必须将另一个客户的信息发送给你。其实每一个客户端也是一个小小的服务器。</p>
<p align="left">所以每一个客户端都和服务器具有相同的结构,除了一点:闲聊服务器还有有一个<font
face="宋体">RMI registry</font>,因为客户端需要依靠闲聊服务器提供对其他所有客户端的引用。<font
face="宋体"> </font></p>
<p align="left">该小程序所用到的<font face="宋体">Java</font>类有:<font face="宋体"><br>
<b>ChatApplet</b> (</font>用于扩展(<font face="宋体">extends</font>)小程序(<font
face="宋体"> Applet</font>)<font face="宋体">) <br>
<b>Chatter</b> (</font>指示外部程序可以调用的方法的接口<font face="宋体">) <br>
<b>ChatterImpl</b> (</font>运行<font face="宋体">Chatter</font>接口<font
face="宋体">) </font></p>
<p align="left">理想情况下,<font face="宋体"><tt>ChatClient</tt></font>和<font face="宋体"><tt>ChatterImpl</tt></font>的功能应该融合到一个<font
face="宋体">Java</font>类中。但由于<font face="宋体">Java</font>只允许你扩展(或者叫继承)一种父类。作为小程序(<font
face="宋体">applet</font>)的<font face="宋体"><tt>ChatApplet</tt></font>必须扩展<font
face="宋体"><tt>Applet</tt></font>以获得<font face="宋体">applet </font>的所有功能。<font
face="宋体"><tt>ChatterImpl</tt></font>必须扩展<font face="宋体"><tt>UnicastRemoteObject</tt></font>以提供<font
face="宋体">RMI</font>服务。所以我们只能通过让<font face="宋体"><tt>ChatApplet</tt></font>获得<font
face="宋体"><tt>ChatterImpl</tt></font>的实例,通过该实例执行方法调用并将数据直接发送给<font
face="宋体"><tt>ChatApplet</tt></font>进行处理。
<p align="left"><font size="3"><font face="宋体">Chatter</font>接口用的方法为:</font>
<p align="left"><font size="3"><tt><font face="宋体">public void receiveEnter(String
name, Chatter chatter) <br>
public void receiveExit(String name) <br>
public void receiveChat(String name, String message) <br>
public void receiveWhisper(String name, String message)</font></tt></font>
<p align="left">该系统关键在于:当客户(比如<font face="宋体" size="3">Joe</font>调用<font face="宋体" size="3"><tt>server.chat("Joe",
"I like Velveeta")</tt></font>时,服务器为系统中每一个客户调用<font face="宋体" size="3"><tt>chatter.receiveChat("Joe",
"I like Velveeta")</tt> </font>
<p align="center"><font face="宋体" size="3"> <img
src="http://static.wired.com/webmonkey/code/97/28/stuff/chat.gif"><br>
anatomy of a chat message </font></p>
<p align="left"><font size="3">使该系统从众多的闲聊系统中脱颖而出的之处在于它有一项私人谈话功能。在本系统中,一个客户可以和另一个客户直接进行私人谈话,而无需通过闲聊服务器进行中转。这样可以减轻服务器的负担,而且你也也可以放心服务器不会偷听你的谈话。<font
face="宋体"> </font></font></p>
<p align="left"><font size="3">但是对此还有安全性的限制。特别是:一个未获得安全认可的<font
face="宋体">applet</font>只能通过主机进行闲聊,而不能直接同另一个客户进行私人谈话,除非你集成了<font
face="宋体">Java 1.1</font>的新特色<font face="宋体"><a
href="http://java.sun.com/products/jdk/1.1/docs/guide/security/">trusted
code</a></font>,在本文中我没有涉及该项。在现有的<font
face="宋体">applet</font>中,客户将的闲聊必须通过其共同的闲聊服务器进行。<font
face="宋体"> </font></font></p>
<p align="left"><font size="3">但神奇的<font face="宋体">RMI</font>的确可以实现私人谈话功能,这里是一个例子:</font></p>
<p align="left"><font size="3">客户<font face="宋体">"Joe" </font>觉得他实在需要同客户<font
face="宋体">"Mary"</font>谈一些私人问题,所以<font face="宋体">Joe</font>需要找到<font
face="宋体">Mary</font>的<font face="宋体">'</font>地址。<font face="宋体"> </font></font></p>
<p align="left"><font size="3">注意<font face="宋体">ChatServer</font>有一个方法叫做<font face="宋体"><tt>Chatter
lookup(String name)</tt></font>。<font face="宋体">Joe</font>可以调用</font>
<ul>
<p align="left"><tt><font face="宋体" size="3">Chatter maryclient
= server.lookup("Mary");</font></tt></p>
</ul>
<p align="left"><font size="3">该方法返回指向<font face="宋体">Mary</font>的<font face="宋体">Chatter</font>对象,于是<font
face="宋体">Joe </font>调用</font>
<div align="left">
<ul>
</ul>
</div>
<ul>
<p align="left"><tt><font face="宋体" size="3">maryclient.receiveWhisper("Joe",
"So, are you a girl?");</font></tt></p>
</ul>
<p align="left"><font face="宋体" size="3"><br>
</font><font size="3">通过该方法,<font face="宋体">Joe</font>就可以偷偷地问<font
face="宋体">mary</font>:“你是个女孩吗?”这个信息于是就会出现在<font
face="宋体">Mary</font>的屏幕上。<font face="宋体"> </font></font></p>
<p align="left"><font face="宋体" size="3"><img
src="http://static.wired.com/webmonkey/code/97/28/stuff/whisper.gif">
<br>
joe</font><font size="3">找到一个亲密伙伴<font face="宋体"><br>
<br>
<br>
</font>如果<font face="宋体">Mary</font>愿意回答这个问题,她就可以通过调用<font
face="宋体"><tt>server.lookup("Joe")</tt></font>继续这个谈话。<font
face="宋体"> </font></font></p>
<p align="left"><font size="3">注意一旦你已经获了有关另一个客户的引用,你应该将其保存下来,不要老是让服务器帮你</font><font
face="宋体" size="3">重复</font><font size="3">查找</font></p>
<p align="left">第<font face="宋体">1</font>页:<a
href="index.html">用<font face="宋体">Java 1.1</font>的<font face="宋体">RMI</font>编写闲聊系统</a><font
face="宋体"><br>
</font>第<font face="宋体">2</font>页:<a href="index2a_chatpage2.htm">服务器</a><font
face="宋体"><br>
</font>第<font face="宋体">3</font>页:<a href="index2a_chatpage3.htm">客户</a><font
face="宋体"><br>
</font> </p>
<p><font size="3"> </font>
<p align="left">
</div>
<!-- #EndEditable --></div>
</td>
<!-- End of search box and directories (columns 2 and 3: middle columns, combined into one) -->
<!-- Gutter between columns 3 and 4 --> <!-- Other stuff (column 4: right column) -->
<!-- End of other stuff (column 4: right column) --> </tr>
</table>
<!-- End of table surrounding page contents -->
<hr noshade size=1 width=700>
<span class=eng><br>
Copyright (C) 1998-2000 Internet Technologies China. All rights reserved.
</span>
</center>
</body>
<!-- #EndTemplate --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -