Month: June 2013

Stream-based Transport

普通的传输是指发送方会在发完一条逻辑消息后,显式地发一个end of stream标识,在接收方眼里,一条消息就是一个packet. 而在Stream-based Transport中,发送方不发送end of stream标识,而只是不停地发字节,接收方需要按照预定义的报文协议自己组合byte,解析出逻辑消息。

NIO – Selector代码示例

package player.kent.chen.temp.learnnio.selector; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; public class PlaySelector { public static void main(String[] args) throws IOException, InterruptedException { ServerSocketChannel serverChannel = ServerSocketChannel.open(); serverChannel.socket().bind(new InetSocketAddress(17547)); serverChannel.configureBlocking(false); Selector selector = Selector.open();//这个selector将同时用于serverChannel和accept()后针对单个连接的socketChannel serverChannel.register(selector, SelectionKey.OP_ACCEPT); //侦听ACCEPT就绪事件 System.out.println("开始侦听连接…"); while (true) { int n = selector.select(); //阻塞直到有事件发生 if …

NIO – Selector代码示例 Read More »

NIO – ServerSocketChannel代码示例

package player.kent.chen.temp.learnnio.socket; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; public class PlayServerSocketChannel { public static void main(String[] args) throws IOException, InterruptedException { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.socket().bind(new InetSocketAddress(17547)); ssc.configureBlocking(false); System.out.println("开始侦听连接…"); while (true) { SocketChannel sc = ssc.accept(); if (sc == null) {//非阻塞模式下,如果没有连接,accept()方法不会阻塞,而是直接返回null Thread.sleep(2000); } else { System.out.println("进来了一个连接。来自:" + sc.socket().getRemoteSocketAddress()); sc.write(ByteBuffer.wrap("尊姓大名: \n".getBytes())); //向客户端要求输入 …

NIO – ServerSocketChannel代码示例 Read More »

NIO – Endian代码示例

package player.kent.chen.temp.learnnio; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class PlayEndian { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.wrap(new byte[20]); buffer.asCharBuffer().put("12345"); printBufferAsArray(buffer); //1, 打印0 49 0 50 0 51 … buffer.rewind(); //让buffer的位置移到最前 buffer.order(ByteOrder.BIG_ENDIAN); buffer.asCharBuffer().put("12345"); printBufferAsArray(buffer); //2, 打印结果将和#1一样,因为java默认的byte order就big endian buffer.rewind(); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.asCharBuffer().put("12345"); printBufferAsArray(buffer); //3,打印49 0 50 0 51 0 … } private static void …

NIO – Endian代码示例 Read More »

NIO – File Channel代码示例

package player.kent.chen.temp.learnnio; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class PlayFileChannel { public static void main(String[] args) throws IOException { playOutput(); playInput(); } private static void playOutput() throws FileNotFoundException, IOException { FileChannel fc = new FileOutputStream("/home/kent/temp/data.txt").getChannel(); ByteBuffer buff = ByteBuffer.wrap("All the data".getBytes()); fc.write(buff); fc.close(); } private static void playInput() …

NIO – File Channel代码示例 Read More »

高效导航

摘自《web导航设计》 1. 导航结构宁可广一点(主栏目数比较多),也不要太深(栏目深达四五层)。 当然,太广了也不好。 2. Don’t make me think 3. 减少用户操作。比如在长页面的尾部提供头部一样的分页导航,否则用户还要滚轮到头部再按下一页。 4. 使用Rich Interaction(ajax),在页内完成功作,减少页面跳转和全局刷新。

表单元素的对齐方式

摘自《web表单设计-点石成金的艺术》 1. 标签顶对齐(即上标签下输入框)   a.好处:用户眼睛只需从上往下移动,这样可以快速完成表单   b.坏处:占用多过垂直空间,导到用户需要向下滚轮 2. 左标签右输入框,标签右对齐   a.好处:由于标签右对齐,存在一条虚拟的中间线;所以用户眼球基本上仍只需从上往下移,完成表单速度仍然较快   b.坏处:标签没有左对齐,不利于快速读完标签内容(因为人们习惯读左对齐的文本);当一个标签本身要占两行时,这个问题更严重 3. 左标签右输入框,标签 左对齐   a.好处:可以快速读完标签内容   b.好处:用户的眼球既要上下滚,又要左右滚,影响表单填写速度 4. 标签以灰色字体放在文本框里作为提示   a.好处:非常节省空间   b.坏处:缺乏始终在场的指导,对健忘的人不利 附作者提供的最佳实践:

首页的内容

摘自《don’t make me think》 首页的内容可以包括: 1. 站点的使命 醒目的logo, 短促响亮的口号,还可以再加一段文字稍长一点的欢迎页面 2. 站点的主要内容 主栏目,可以加上二级栏目 3. 搜索 有的人希望立即找到自己想要的东西 4. 导读 把精彩内容放在杂志封面 5. 内容更新 本站不是一滩死水 6. Quick Links 7. 注册/登录

一些提高表单交互水平的技巧

摘自《web表单设计-点石成金的艺术》 1. 表单页面少弄些无关的东西,以免影响注意力。 比如在电商下单页面搞很多关联商品推荐,很容易导致用户放弃下单,去看你推荐的商品。 2. 对于多页表单,应该提供导航栏,列出所有步骤,并指出当前步骤。 3. 输入框的长度该短就要短,这样可以起到良好的暗示作用。比如邮政编码只允许输入6位。 4. 在用户还在输入汉字或英文时就提供反馈,提高交互效率。比如    a.即时验证    b.遇"chi"提示"china", 让用户选择即可    c.提示还“允许输入80字”    d.使用弹出层,比如日期控件或其他富对话框。

持久导航 – 每页都要有的导航条

摘自《don’t make me think》 持久导航 – 每页都要有的导航: 但有两处可以例外: 1. 首页:首页比较特殊,可以有自己的编排方式 2. 表单页面: 表单页面应该尽量无噪声,以免用户注意力被干扰