最近小弟在用websocket做WEB聊天应用,目前为止,支持离线消息等,感觉还行,不过很蛋疼的是文件传输和语音视频等就麻烦了啊!!!好了,言规正传,下面介绍下在Jetty官网上看到的新闻消息,额,,,小弟英文不好,就不翻译了啊,只大概介绍下,如有错误,还启望指正,此举意在有兴趣的一起探讨下,勿喷!
首先,9.x与8.x在websocket的构建上都比较容易,如果只是简单的应用,那么在jetty8.x上只需要一个websocketservlet和一个实现了onTextMessage(或另外一个接口)接口的类就可以了。不过到了9.x有了一点改动,与以前相比,我认为更合理,同样它需要一个websocketservlet,但实现的接口与之前不一样了,是实现了WebSocketListener这样一个接口,废话少话,下面给官网的demo吧:
package examples; import org.eclipse.jetty.websocket.server.WebSocketServerFactory; import org.eclipse.jetty.websocket.server.WebSocketServlet; public class MyEchoServlet extends WebSocketServlet { @Override public void configure(WebSocketServerFactory factory) { // register a socket class as default factory.register(MyEchoSocket.class); } }
下面再是websocket的接口实现类:
package examples; import java.io.IOException; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.websocket.core.api.WebSocketConnection; import org.eclipse.jetty.websocket.core.api.WebSocketException; import org.eclipse.jetty.websocket.core.api.WebSocketListener; public class MyEchoSocket implements WebSocketListener { private WebSocketConnection outbound; @Override public void onWebSocketBinary(byte[] payload, int offset, int len) { /* only interested in text messages */ } @Override public void onWebSocketClose(int statusCode, String reason) { this.outbound = null; } @Override public void onWebSocketConnect(WebSocketConnection connection) { this.outbound = connection; } @Override public void onWebSocketException(WebSocketException error) { error.printStackTrace(); } @Override public void onWebSocketText(String message) { if (outbound == null) { return; } try { String context = null; Callback callback = new FutureCallback<>(); outbound.write(context,callback,message); } catch (IOException e) { e.printStackTrace(); } } }
这样的话当客户端发送普通文本消息就会走onSocketText方法,当发送二进制文件时就会走onWebSocketBinary,感觉这样开发起来更方便,更好,比如,客户端发送了语音消息就自动走onWebSocketBinary方法,发送文本消息就自动 走onSocketText方法,对我们开发人员来说更容易处理。当然除了实现接口,还有其它的方法,继续一个WebSocketAdapter类,有点像GUI编程里面的监听器和监听器实现类,呵呵。。。这种继续方式就不多做介绍了,有兴趣的就一起看下吧。呵呵。。。这是介绍地址:http://webtide.intalio.com/2012/10/jetty-9-updated-websocket-api/
引用来自“yak”的评论
import org.eclipse.jetty.websocket.server.WebSocketServlet; 这个在哪个jar里面,lib没找到
引用来自“谭耀武”的评论
引用来自“张浩春”的评论
引用来自“谭耀武”的评论
最近用cometd3.0.0.beta2+jetty9.1.1+jquery.cometd做了个聊天室,发现刷新浏览器时(大体上,刷新时先会关闭websocket连接,然后马上连接websocket),会报下面这样的错,楼主是否有遇到过?这个原因基本我大体也清楚,就是不知道有没有办法能规避掉。
java.io.IOException: Write is closed
at org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:147) ~[websocket-common-9.1.1.v20140108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:473) [websocket-common-9.1
.1.v20140108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.extensions.AbstractExtension.nextOutgoingFrame(AbstractExtension.java:168) [websocket-common-9.1.1.v2014
0108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension.nextOutgoingFrame(PerMessageDeflateExtension.java:293) [w
ebsocket-common-9.1.1.v20140108.jar:9.1.1.v20140108]
引用来自“张浩春”的评论
引用来自“谭耀武”的评论
最近用cometd3.0.0.beta2+jetty9.1.1+jquery.cometd做了个聊天室,发现刷新浏览器时(大体上,刷新时先会关闭websocket连接,然后马上连接websocket),会报下面这样的错,楼主是否有遇到过?这个原因基本我大体也清楚,就是不知道有没有办法能规避掉。
java.io.IOException: Write is closed
at org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:147) ~[websocket-common-9.1.1.v20140108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:473) [websocket-common-9.1
.1.v20140108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.extensions.AbstractExtension.nextOutgoingFrame(AbstractExtension.java:168) [websocket-common-9.1.1.v2014
0108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension.nextOutgoingFrame(PerMessageDeflateExtension.java:293) [w
ebsocket-common-9.1.1.v20140108.jar:9.1.1.v20140108]
引用来自“谭耀武”的评论
最近用cometd3.0.0.beta2+jetty9.1.1+jquery.cometd做了个聊天室,发现刷新浏览器时(大体上,刷新时先会关闭websocket连接,然后马上连接websocket),会报下面这样的错,楼主是否有遇到过?这个原因基本我大体也清楚,就是不知道有没有办法能规避掉。
java.io.IOException: Write is closed
at org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:147) ~[websocket-common-9.1.1.v20140108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:473) [websocket-common-9.1
.1.v20140108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.extensions.AbstractExtension.nextOutgoingFrame(AbstractExtension.java:168) [websocket-common-9.1.1.v2014
0108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension.nextOutgoingFrame(PerMessageDeflateExtension.java:293) [w
ebsocket-common-9.1.1.v20140108.jar:9.1.1.v20140108]
java.io.IOException: Write is closed
at org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:147) ~[websocket-common-9.1.1.v20140108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:473) [websocket-common-9.1
.1.v20140108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.extensions.AbstractExtension.nextOutgoingFrame(AbstractExtension.java:168) [websocket-common-9.1.1.v2014
0108.jar:9.1.1.v20140108]
at org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension.nextOutgoingFrame(PerMessageDeflateExtension.java:293) [w
ebsocket-common-9.1.1.v20140108.jar:9.1.1.v20140108]
引用来自“wms1”的评论
引用来自“张浩春”的评论
引用来自“wms1”的评论
有关jetty websocket的类全部都说not found
引用来自“张浩春”的评论
引用来自“wms1”的评论
有关jetty websocket的类全部都说not found
引用来自“wms1”的评论
有关jetty websocket的类全部都说not found
引用来自“wms1”的评论
运行出错,说ClassNotFound,但文件明明存在!!!怎么解决?
引用来自“funsunz”的评论
目前为止,哪些浏览器支持呢?