- 浏览: 1449340 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (691)
- linux (207)
- shell (33)
- java (42)
- 其他 (22)
- javascript (33)
- cloud (16)
- python (33)
- c (48)
- sql (12)
- 工具 (6)
- 缓存 (16)
- ubuntu (7)
- perl (3)
- lua (2)
- 超级有用 (2)
- 服务器 (2)
- mac (22)
- nginx (34)
- php (2)
- 内核 (2)
- gdb (13)
- ICTCLAS (2)
- mac android (0)
- unix (1)
- android (1)
- vim (1)
- epoll (1)
- ios (21)
- mysql (3)
- systemtap (1)
- 算法 (2)
- 汇编 (2)
- arm (3)
- 我的数据结构 (8)
- websocket (12)
- hadoop (5)
- thrift (2)
- hbase (1)
- graphviz (1)
- redis (1)
- raspberry (2)
- qemu (31)
- opencv (4)
- socket (1)
- opengl (1)
- ibeacons (1)
- emacs (6)
- openstack (24)
- docker (1)
- webrtc (11)
- angularjs (2)
- neutron (23)
- jslinux (18)
- 网络 (13)
- tap (9)
- tensorflow (8)
- nlu (4)
- asm.js (5)
- sip (3)
- xl2tp (5)
- conda (1)
- emscripten (6)
- ffmpeg (10)
- srt (1)
- wasm (5)
- bert (3)
- kaldi (4)
- 知识图谱 (1)
最新评论
-
wahahachuang8:
我喜欢代码简洁易读,服务稳定的推送服务,前段时间研究了一下go ...
websocket的helloworld -
q114687576:
http://www.blue-zero.com/WebSoc ...
websocket的helloworld -
zhaoyanzimm:
感谢您的分享,给我提供了很大的帮助,在使用过程中发现了一个问题 ...
nginx的helloworld模块的helloworld -
haoningabc:
leebyte 写道太NB了,期待早日用上Killinux!么 ...
qemu+emacs+gdb调试内核 -
leebyte:
太NB了,期待早日用上Killinux!
qemu+emacs+gdb调试内核
ChatClient.java
ChatServer.java
EmptyClient.java
WebSocket.jar
index.html
EmptyClient.java
ChatServer.java
ChatServer.java
EmptyClient.java
WebSocket.jar
index.html
index.html <!DOCTYPE html> <html> <head> <title>WebSocket Chat Client</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="prototype.js"></script> <script type="text/javascript"> document.observe("dom:loaded", function() { function log(text) { $("log").innerHTML = (new Date).getTime() + ": " + (!Object.isUndefined(text) && text !== null ? text : "null") +"<br>"+ $("log").innerHTML; } if (!window.WebSocket) { alert("FATAL: WebSocket not natively supported. This demo will not work!"); } var ws; $("uriForm").observe("submit", function(e) { e.stop(); ws = new WebSocket($F("uri")); ws.onopen = function() { log("[WebSocket#onopen]"); } ws.onmessage = function(e) { log("<b style='color:red;'>[WebSocket#onmessage] 接收消息:" + e.data + "</b>"); } ws.onclose = function() { log("[WebSocket#onclose]"); $("uri", "connect").invoke("enable"); $("disconnect").disable(); ws = null; } $("uri", "connect").invoke("disable"); $("disconnect").enable(); }); $("sendForm").observe("submit", function(e) { e.stop(); if (ws) { var textField = $("textField"); ws.send(textField.value); //log("[WebSocket#send] 发送消息:" + textField.value ); textField.value = ""; textField.focus(); } }); $("clear").observe("click", function(e) { $("log").innerHTML =''; }); $("disconnect").observe("click", function(e) { e.stop(); if (ws) { ws.close(); ws = null; } }); }); </script> </head> <body> <form id="uriForm"><input type="text" id="uri" value="ws://localhost:8887" style="width:200px;"> <input type="submit" id="connect" value="Connect"><input type="button" id="disconnect" value="Disconnect" disabled="disabled"></form><br> <form id="sendForm"><input type="text" id="textField" value="" style="width:200px;"> <input type="submit" value="Send"> <input type="button" id='clear' value="clear msg"></form><br> <form><div id="log" style="width:800px;heigth:450px" ></div></form><br> </body> </html>
EmptyClient.java
import java.net.URI; import org.java_websocket.WebSocketClient; import org.java_websocket.drafts.Draft; import org.java_websocket.handshake.ServerHandshake; public class EmptyClient extends WebSocketClient { public EmptyClient( URI serverUri , Draft draft ) { super( serverUri, draft ); } public EmptyClient( URI serverURI ) { super( serverURI ); } @Override public void onOpen( ServerHandshake handshakedata ) { } @Override public void onMessage( String message ) { } @Override public void onClose( int code, String reason, boolean remote ) { } @Override public void onError( Exception ex ) { } }
ChatServer.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.util.Set; import org.java_websocket.WebSocket; import org.java_websocket.WebSocketServer; import org.java_websocket.handshake.ClientHandshake; public class ChatServer extends WebSocketServer { public ChatServer( int port ) throws UnknownHostException { super( new InetSocketAddress( InetAddress.getByName( "localhost" ), port ) ); } public ChatServer( InetSocketAddress address ) { super( address ); } @Override public void onOpen( WebSocket conn, ClientHandshake handshake ) { try { this.sendToAll( "new" ); } catch ( InterruptedException ex ) { ex.printStackTrace(); } System.out.println( conn + " entered the room!" ); } @Override public void onClose( WebSocket conn, int code, String reason, boolean remote ) { try { this.sendToAll( conn + " has left the room!" ); } catch ( InterruptedException ex ) { ex.printStackTrace(); } System.out.println( conn + " has left the room!" ); } @Override public void onMessage( WebSocket conn, String message ) { try { this.sendToAll( message ); } catch ( InterruptedException ex ) { ex.printStackTrace(); } System.out.println( conn + ": " + message ); } @Override public void onError( WebSocket conn, Exception ex ) { ex.printStackTrace(); } public void sendToAll( String text ) throws InterruptedException { Set<WebSocket> con = connections(); synchronized ( con ) { for( WebSocket c : con ) { c.send( text ); } } } /////////////////////////////////////////////////////////////////////////////////////// public static void main( String[] args ) throws InterruptedException , IOException { //连接部份 WebSocket.DEBUG = true; int port = 8887; try { port = Integer.parseInt( args[ 0 ] ); } catch ( Exception ex ) { } ChatServer s = new ChatServer( port ); s.start(); System.out.println( "ChatServer started on port: " + s.getPort() ); //服务端 发送消息处理部份 BufferedReader sysin = new BufferedReader( new InputStreamReader( System.in ) ); while ( true ) { String in = sysin.readLine(); s.sendToAll( in ); } } }
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.net.URI; import java.net.URISyntaxException; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import org.java_websocket.WebSocket; import org.java_websocket.WebSocketClient; import org.java_websocket.drafts.Draft; import org.java_websocket.drafts.Draft_10; import org.java_websocket.drafts.Draft_17; import org.java_websocket.drafts.Draft_75; import org.java_websocket.drafts.Draft_76; import org.java_websocket.handshake.ServerHandshake; public class ChatClient extends JFrame implements ActionListener { private static final long serialVersionUID = -6056260699202978657L; private final JTextField uriField; private final JButton connect; private final JButton close; private final JTextArea ta; private final JTextField chatField; private final JComboBox draft; private WebSocketClient cc; public ChatClient( String defaultlocation ) { super( "WebSocket Chat Client" ); Container c = getContentPane(); GridLayout layout = new GridLayout(); layout.setColumns( 1 ); layout.setRows( 6 ); c.setLayout( layout ); Draft[] drafts = { new Draft_17(), new Draft_10(), new Draft_76(), new Draft_75() }; draft = new JComboBox( drafts ); c.add( draft ); uriField = new JTextField(); uriField.setText( defaultlocation ); c.add( uriField ); connect = new JButton( "Connect" ); connect.addActionListener( this ); c.add( connect ); close = new JButton( "Close" ); close.addActionListener( this ); close.setEnabled( false ); c.add( close ); JScrollPane scroll = new JScrollPane(); ta = new JTextArea(); scroll.setViewportView( ta ); c.add( scroll ); chatField = new JTextField(); chatField.setText( "" ); chatField.addActionListener( this ); c.add( chatField ); java.awt.Dimension d = new java.awt.Dimension( 300, 400 ); setPreferredSize( d ); setSize( d ); addWindowListener( new java.awt.event.WindowAdapter() { @Override public void windowClosing( WindowEvent e ) { if( cc != null ) { cc.close(); } dispose(); } } ); setLocationRelativeTo( null ); setVisible( true ); } public void actionPerformed( ActionEvent e ) { if( e.getSource() == chatField ) { if( cc != null ) { try { cc.send( chatField.getText() ); chatField.setText( "" ); chatField.requestFocus(); } catch ( InterruptedException ex ) { ex.printStackTrace(); } } } else if( e.getSource() == connect ) { try { // cc = new ChatClient(new URI(uriField.getText()), area, ( Draft ) draft.getSelectedItem() ); cc = new WebSocketClient( new URI( uriField.getText() ), (Draft) draft.getSelectedItem() ) { @Override public void onMessage( String message ) { ta.append( "got: " + message + "\n" ); ta.setCaretPosition( ta.getDocument().getLength() ); } @Override public void onOpen( ServerHandshake handshake ) { ta.append( "You are connected to ChatServer: " + getURI() + "\n" ); ta.setCaretPosition( ta.getDocument().getLength() ); } @Override public void onClose( int code, String reason, boolean remote ) { ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" ); ta.setCaretPosition( ta.getDocument().getLength() ); connect.setEnabled( true ); uriField.setEditable( true ); draft.setEditable( true ); close.setEnabled( false ); } @Override public void onError( Exception ex ) { ta.append( "Exception occured ...\n" + ex + "\n" ); ta.setCaretPosition( ta.getDocument().getLength() ); ex.printStackTrace(); connect.setEnabled( true ); uriField.setEditable( true ); draft.setEditable( true ); close.setEnabled( false ); } }; close.setEnabled( true ); connect.setEnabled( false ); uriField.setEditable( false ); draft.setEditable( false ); cc.connect(); } catch ( URISyntaxException ex ) { ta.append( uriField.getText() + " is not a valid WebSocket URI\n" ); } } else if( e.getSource() == close ) { try { cc.close(); } catch ( Exception ex ) { ex.printStackTrace(); } } } public static void main( String[] args ) { WebSocket.DEBUG = true; String location; if( args.length != 0 ) { location = args[ 0 ]; System.out.println( "Default server url specified: \'" + location + "\'" ); } else { location = "ws://localhost:8887"; System.out.println( "Default server url not specified: defaulting to \'" + location + "\'" ); } new ChatClient( location ); } }
- prototype.js.png (136.6 KB)
- 下载次数: 174
- WebSocket.jar.png (67.4 KB)
- 下载次数: 201
评论
2 楼
wahahachuang8
2017-01-22
我喜欢代码简洁易读,服务稳定的推送服务,前段时间研究了一下goeasy,java后台推送只需要两行代码, js前端推送也只需要3,4行,而且文档齐全,还提供了后台查询信息收发情况,所以我觉得GoEasy推送服务是个不错的选择。官网: https://goeasy.io/
1 楼
q114687576
2016-03-16
http://www.blue-zero.com/WebSocket/ 在线WebSocket测试工具,完全兼容IE6及以上版本,超过128字节正常收发。
发表评论
-
websocket直播
2020-05-22 17:59 4391.websocket转发的最简单server 2.h5接收w ... -
openresty聊天室的helloworld
2018-04-22 19:25 728openresty的websocket + redis的sub ... -
openresty websocket
2018-04-18 17:08 1423mac安装openresty brew install o ... -
nginx代理wss和https
2018-02-27 15:34 3870nginx启用ssl yum install openssl ... -
websocket和tap使用select关联
2016-06-14 22:01 723c语言的socket基础http://haoningabc.i ... -
websocket传传图片
2015-12-25 17:51 7213参考[url]http://www.adobe.com/dev ... -
websocket相关调研 总结帖
2014-12-21 21:53 3950最近把websocket的客户端和服务端使用过的调通的例子总结 ... -
autobahn的helloworld
2014-11-08 18:36 2724python2.7.8可用,python2.6一样的代码就有问 ... -
ios websocket server端
2014-10-22 00:07 2158https://github.com/benlodotcom/ ... -
ios客户端websocket的helloworld
2014-10-09 02:11 23152ios8,xcode6 https://github.com/ ... -
websocket,使用tomcat7转发
2014-10-03 18:40 11646前篇: http://haoningabc.iteye.com ...
相关推荐
一个最简单的websocket的HelloWorld
使用SpringMVC结合WebSocket实现服务器与客户端双向沟通,程序很简单,只是为了演示配置(hello world),IntelliJ Maven项目,部署好以后测试地址 http://localhost:8080/test
GRPC-Bus WebSocket代理客户端该客户端库通过WebSocket代理服务器将浏览器JavaScript...new GBC ( "ws://localhost:8080/" , 'helloworld.proto' , { helloworld : { Greeter : 'localhost:50051' } } ) . connect ( )
express-websocket 使用 websocket 连接访问快速路由。 安装 npm install psi-4ward/express-websocket 服务器 // Init express var app = require ( 'express' ) ( ) ;... send ( 'Hello World!' ) } ) ; var http
node.js 的socket.io的使用,适合nodejs初学者,超精简的node.js websocket网页聊天室DEMO,虽然是很基础,但你至少得会配置nodejs,会敲hello world吧。
该项目现在包含以下Hello World示例:1-SOAP Web服务(基于:@WebService的批注)2-RESTFULL Web服务3-WebSocket 1-JSF Hello World URL: 相关的Java类:-com.arnia.web.jsf.bean; 相关xhtml文件:-faces / hello....
ws.send("hello world"); 在网络选项卡中,显示 WebSocket 框架 定义消息处理程序 ws.onmessage = function(evt) { console.log(evt) } 或者 ws.onmessage = function(evt) { alert(evt.data) } 从 ...
Node.js + Socket.io - Hello World 应用程序创建此演示应用程序是为了测试智能电视浏览器的兼容性,但也可用于其他目的。什么是 Socket.io? Socket.io 是一个跨浏览器的 Websocket 类 API。 它由客户端和服务器端...
yii-将yii与websocket一起使用的示例:Yii :: app()-> websocket-> send('Hello world'); 游戏-简单的游戏 game2-游戏(node.js游戏端口: : ) 从控制台运行: 开始:“ php index.php开始”或“ nohup ...
本指南将引导您完成创建“Hello, world”应用程序的过程,该应用程序在浏览器和服务器之间来回发送消息。 WebSocket 是 TCP 之上的一个轻量级薄层。 这使它适合使用“子协议”来嵌入消息。 在本指南中,我们使用 ...
Serving Hello World Eventing Hello World Tekton Hello World Serving 进阶 自动扩缩容 - Autoscaler Serving 健康检查机制分析 流量灰度和版本管理 服务路由管理 WebSocket 和 gRPC 服务 Serving Client 介绍 ...
messageport-websocket 该软件包会将转换为 。 背景 与其为不同的通信渠道学习/使用不同的API,我们应该将它们... send ( 'Hello, World!' ) ; 除了订阅onmessage ,您还可以使用on('message', handler)订阅。 请注
Websockets 播放演示目标 - 使用网络套接字服务器应该在 Java Play 框架中在 Python 中制作一个外部脚本,它想通过 websockets 与客户端通信在 AngularJs 中制作前端任务让 Play websockets Hello World 与外部应用...
它创建了一个侦听端口8080并具有简单的“ Hello world!”的Web服务器。 如果打开Web浏览器并打开URL 响应。 启动服务器: 节点wsserver.jsclient.js client.js脚本将创建到主机服务器的原始TCP / IP套接字连接,...
1.netty入门 -- netty-helloworld 2.netty的粘包 - netty-stick 3.netty支持的各协议,包含messagepack、protobuf以及私有协议 - netty-protocol 4.netty开发httpserver服务 - netty-httpserver 5.netty开发...
符合服务器和客户端的一致,零依赖,惰性,简单的 。 入门 安装 $ yarn add graphql-ws ... hello : ( ) => 'Hello World!' , } , subscription : { greetings : async function * sayHiIn5Languages ( ) { for ( c
[的Hello World](数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAXgAAAMMCAYAAABZsbxYAAAABHNCSVQICAgIfAhkiAAAABl0RVh0 U29mdHdhcmUAZ25vbWUtc2NyZWVuc2hvdO8Dvz4AACAASURBVHic7N13mBRF + sDxb3dP3sDukkEy ...
... 支持多种路由,路由可分组,路由匹配支持正则模式. 支持middleware机制,可在任意路由上挂载中间件. ... local lor = require("lor.index") ... res:send("hello world!") end) app:run() 标签:Web框架
重大更改支持批处理过程websocket的实验支持英文教程日语[Kagura] Kagura + RustでWebページを作成Hello World extern crate kagura; 外箱wasm_bindgen; 使用kagura :: prelude :: *; 使用wasm_bindgen :: prelude :...
第 1-4 课:写一个 Hello World 来感受 Spring Boot/hello 第 2-1 课: Spring Boot 对基础 Web 开发支持/spring-boot-web 第 2-10 课: 使用 Spring Boot WebSocket 创建聊天室/spring-boot-websocket 第 2-2 课...