- 浏览: 1477762 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (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 5081.websocket转发的最简单server 2.h5接收w ... -
openresty聊天室的helloworld
2018-04-22 19:25 799openresty的websocket + redis的sub ... -
openresty websocket
2018-04-18 17:08 1519mac安装openresty brew install o ... -
nginx代理wss和https
2018-02-27 15:34 3929nginx启用ssl yum install openssl ... -
websocket和tap使用select关联
2016-06-14 22:01 749c语言的socket基础http://haoningabc.i ... -
websocket传传图片
2015-12-25 17:51 7246参考[url]http://www.adobe.com/dev ... -
websocket相关调研 总结帖
2014-12-21 21:53 4028最近把websocket的客户端和服务端使用过的调通的例子总结 ... -
autobahn的helloworld
2014-11-08 18:36 2765python2.7.8可用,python2.6一样的代码就有问 ... -
ios websocket server端
2014-10-22 00:07 2198https://github.com/benlodotcom/ ... -
ios客户端websocket的helloworld
2014-10-09 02:11 23188ios8,xcode6 https://github.com/ ... -
websocket,使用tomcat7转发
2014-10-03 18:40 11740前篇: http://haoningabc.iteye.com ...
相关推荐
7. **运行并测试**:启动Tomcat服务器,通过浏览器访问`http://localhost:8080/HelloWorld/hello`(假设你的请求映射为"/hello"),如果一切配置正确,你应该能看到“Hello World”显示在页面上。 通过这个简单的...
stompClient.send("/app/hello", {}, JSON.stringify({'name': 'World'})); } function showGreeting(message) { $('#greetings').append('<tr><td>' + message + '</td></tr>'); } }); ``` 这段代码实现了...
在提供的"HelloWorld"示例中,可能包含了一个简单的WebSocket实现,展示了如何在Django中创建一个基础的WebSocket服务。这个例子可能包含了一个消费者类,用于接收和发送“Hello, World!”的消息,以及一个前端页面...
3. 基础API示例:文档提供了一个简单的客户端示例,该客户端通过WebSocket发送“Hello, World!”。以及一个简单的回声服务器示例,该服务器接收客户端的消息并将其发送回去。 4. Python版本要求:websockets库要求...
JavaScript 代码 Hello World 详解 本文将对 JavaScript 代码 Hello World 1 进行详细解释,涵盖 HTML、JavaScript、事件处理等多个方面。 HTML 结构 首先,让我们看一下 HTML 结构 <!DOCTYPE ...
5 System.out.println("Hello World"); 6 } 7 } ``` #### 6. 非标准注释 非标准的注释格式如下所示: ``` 注释:这是一个注释。 ``` #### 7. 专家组成员 Java WebSocket 规范是在Java社区进程中开发的,作为JSR ...
stompClient.send("/app/hello", {}, JSON.stringify({'name': 'World'})); }, function(error) { console.error(error); }); ``` ### 5. 安全性与优化 为了确保WebSocket连接的安全,我们可能需要添加身份验证...
描述中的"一个最简单的websocket的HelloWorld"意味着这个压缩包可能包含了一个用于展示WebSocket基本用法的入门级代码示例。通常,"Hello, World!"程序是编程初学者的第一个教程,用于演示如何在新的编程语言或技术...
stompClient.send('/app/hello', {}, JSON.stringify({'name': 'World'})); }); ``` 这个例子展示了如何在Spring Boot中使用WebSocket和STOMP进行实时通信。客户端发送一个消息到`/app/hello`,服务器接收到消息后...
最后,为了测试我们的即时通讯功能,可以创建一个简单的HelloWorld示例。例如,当用户发送一条消息时,服务器将这条消息广播回所有连接的客户端,实现基本的群聊功能。 在实际应用中,我们还需要考虑更多的细节,如...
stompClient.send("/app/hello", {}, JSON.stringify({'name': 'World'})); }); function showGreeting(content) { // 在页面上显示问候内容 } ``` 通过以上步骤,我们就成功地在Spring Boot项目中集成了...
本“dojo服务器推送技术的HelloWorld例子”旨在帮助初学者理解如何使用Dojo和Comet技术来实现实时通信。下面我们将深入探讨这两个关键概念。 1. **Dojo框架**: Dojo是一个开源的JavaScript库,提供了丰富的功能和...
stompClient.send("/app/hello", {}, JSON.stringify({'name': 'World'})); }); function showGreeting(message) { var g = document.getElementById('greetings'); var el = document.createElement('div');...
<entry-point class='com.example.HelloWorld' /> ``` ### 资源管理 PlayN-samples压缩包中包含了一些示例项目,你可以通过学习这些示例来理解如何管理游戏资源,如图像、音频和字体。每个示例都会展示PlayN提供...
stompClient.send('/app/hello', {}, JSON.stringify({'name': 'World'})); stompClient.subscribe('/topic/greetings', function(greeting){ showGreeting(JSON.parse(greeting.body).content); }); }); ``` ...
socket.emit('chatMessage', { user: 'Alice', text: 'Hello, world!' }); ``` 服务器端处理事件: ```javascript socket.on('chatMessage', (msg) => { console.log(`用户${msg.user}发送的消息: ${msg.text...
使用SpringMVC结合WebSocket实现服务器与客户端双向沟通,程序很简单,只是为了演示配置(hello world),IntelliJ Maven项目,部署好以后测试地址 http://localhost:8080/test
- 示例2:服务器发送一个消息'HelloWorld',客户端接收到后同样调用回调函数处理数据。 - 'upgrade'表示Engine.IO正在尝试升级到WebSocket连接。 - 'noop'是无操作的占位符,可能用于心跳或保持连接活跃。 - '...
socket.send(JSON.stringify({type: 'message', content: 'Hello, world!'})); ``` 消息通常被序列化为JSON格式,便于服务器解析处理。在这个聊天应用中,可能有不同类型的消息,如用户输入的文字、用户加入房间的...
GRPC-Bus WebSocket代理客户端该客户端库通过WebSocket代理服务器将浏览器JavaScript...new GBC ( "ws://localhost:8080/" , 'helloworld.proto' , { helloworld : { Greeter : 'localhost:50051' } } ) . connect ( )