IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Java WebSocket编程式端点 -> 正文阅读

[网络协议]Java WebSocket编程式端点

package server.ws;

import java.io.IOException;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

public class ProgrammaticEchoServer extends Endpoint {

	@Override
	public void onOpen(Session session, EndpointConfig endpointConfig) {
		final Session mySession = session;
		mySession.addMessageHandler(new MessageHandler.Whole<String>() {
			@Override
			public void onMessage(String text) {
				try {
					mySession.getBasicRemote().sendText("I got this (" + text + ") so I am sending it back !");
				} catch (IOException ioe) {
					System.out.println(
							"oh dear, something went wrong trying to send the message back: " + ioe.getMessage());
				}
			}
		});
	}

}
package server.ws;

import java.util.HashSet;
import java.util.Set;
import javax.websocket.Endpoint;
import javax.websocket.server.*;

public class ProgrammaticEchoServerAppConfig implements ServerApplicationConfig {

	@Override
	public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> endpointClasses) {
		Set configs = new HashSet<ServerEndpointConfig>();
		ServerEndpointConfig sec = ServerEndpointConfig.Builder
				.create(ProgrammaticEchoServer.class, "/programmaticecho").build();
		configs.add(sec);
		return configs;
	}

	@Override
	public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {
		return scanned;
	}
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Web Socket JavaScript Echo Client</title>
<script language="javascript" type="text/javascript">
	var echo_websocket;
	function init() {
		output = document.getElementById("output");
	}
	function send_echo() {
		var wsUri = "ws://localhost:8080/WebSocketServerExample/programmaticecho";
		writeToScreen("Connecting to " + wsUri);
		echo_websocket = new WebSocket(wsUri);
		echo_websocket.onopen = function(evt) {
			writeToScreen("Connected !");
			doSend(textID.value);
		};
		echo_websocket.onmessage = function(evt) {
			writeToScreen("Received message: " + evt.data);
			echo_websocket.close();
		};
		echo_websocket.onerror = function(evt) {
			writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
			echo_websocket.close();
		};
	}
	function doSend(message) {
		echo_websocket.send(message);
		writeToScreen("Sent message: " + message);
	}
	function writeToScreen(message) {
		var pre = document.createElement("p");
		pre.style.wordWrap = "break-word";
		pre.innerHTML = message;
		output.appendChild(pre);
	}
	window.addEventListener("load", init, false);
</script>
</head>
<body>
	<h1>Echo Server</h1>
	<div style="text-align: left;">
		<form action="">
			<input onclick="send_echo()" value="Press to send" type="button">
			<input id="textID" name="message" value="Hello Web Sockets"
				type="text"> <br>
		</form>
	</div>
	<div id="output"></div>
</body>
</html>

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-08-01 14:49:27  更:2021-08-01 14:51:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 10:59:41-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码