websocket的握手与数据传输

关于握手,转一张 图: 连接建立后,客户端和服务端就可以 全双工地通迅。 数据传输以"message"为单位. 一个message由一个或多个frame组成,每个frame等于一个 header + payload.

Netty的websocket例子

看这个官方示例: http://netty.io/3.5/xref/org/jboss/netty/example/http/websocketx/server/package-summary.html 不要被类名疑惑。它既包含了服务端,也通过产生js代码、提供了客户端。 java客户端: https://github.com/netty/netty/tree/master/example/src/main/java/io/netty/example/http/websocketx/client

关于comet、websocket的比较

首先要明白websocket不是http协议,只不过有些相似的地方: 报文格式相似(但不相同),都被浏览器支持,服务商品都是80等。 而comet是基于http协议的. 再推荐一篇文章,里面总结了websocket相比于comet在性能方面的优点: Ajax、Comet、HTML 5 Web Sockets技术比较分析

comet

摘自wiki: Comet is an umbrella term, encompassing multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins. The Comet approach differs from the original model of the web, in which a browser requests a complete web page at a time. …

comet Read More »

NIO – Netty代码示例

以下代码代表一个加法。客户端提交加数,服务端回送“和”。 服务端 package player.kent.chen.temp.learnnetty.raw; import java.net.InetSocketAddress; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.group.ChannelGroup; import org.jboss.netty.channel.group.ChannelGroupFuture; import org.jboss.netty.channel.group.DefaultChannelGroup; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** * 报文格式:request=a+b, response=c * * @author 陈坚 2013年6月18日下午2:47:04 */ public class PlayNettyAddServer …

NIO – Netty代码示例 Read More »

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 »