java websocket pdf_Java WebSocket Programming.pdf

【实例简介】

英文版本,前台 与 服务器之间通讯技术

ORACLE FL Java Web Socket Programming /Danny Coward/719-6/FM

About the author

Danny Coward is a Chief Architect and Web architect at Oracle. He is the

Specification Lead for the Java API for Web Socket for Java EE and Java se/

JavaFX Cowards work leading Web Sockets at Oracle makes him the leading

expert on Java Web Socket programming. Coward has specialized experience

in all aspects of Java software--from Java me to Java ee to the founding of

the JavaFX technology

About the Technical Editor

Dr Santiago Pericas-Geertsen is a Principal Member of Technical Staff in

the Sun glassfish organization at Oracle, and an architect and technical lead

in the Avatar project. Santiago is a Specification Lead for JSR 339, JAX-RS

2.0. While at Sun Microsystems, Santiago was a technical lead for the

Glassfish Mobility platform, a developer and lead in the fast Web Services

project, and a participant and editor in World Wide Web Consortium(W3C

initiatives. He holds two US patents, 7647415 and 7716577 Santiago

blogs from Java. net, tweets from @spericas, and has presented at numerous

academic and industry-oriented conferences

00-FM indd 2

8/7/135:59PM

ORACLE FL Java Web Socket Programming /Danny Coward/719-6/FM

ORACL∈

O

TM

racle

PPr

Java Web Socket

Programming

Danny Coward

Mc

Graw

Education

New York Chicago San francisco

Athens London Madrid Mexico City

Milan New Delhi Singapore Sydney Toronto

00-FM indd 3

8/8/1312:06PM

Copyright o 2014 by McGraw-Hill Education(Publisher). All rights reserved. Printed in the United States of America

Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any

form or by any means, or stored in a database or retrieval system, without the prior written permission of publisher, with

the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be

reproduced for publication

ISBN:978-0-07-182718-8

MHID:0-07-182718-8

e-book conversion by Cenveo Publisher Services

sion 1.0

The material in this e-book also appears in the print version of this title: ISBN: 978-0-07-182719-5

MHID:0-07-182719-6

AcGraw-Hill Education e-books are available at special quantity discounts to use as premiums and sales

promotions, or for use in corporate training programs. To contact a representative, please visit the Contact U

pagesatwww.mhprofessional.com

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. All other trademarks are the property of thei

respective owners, and McGraw-Hill Education makes no claim of ownership by the mention of products that contain

these marks

Screen displays of copyrighted Oracle software programs have been reproduced herein with the permission of oracle

Corporation and/or its affiliates

Information has been obtained by McGraw-Hill Education from sources believed to be reliable. However, because of the

possibility of human or mechanical error by our sources, McGraw-Hill Education, or others, McGraw-Hill Education

does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or

omissions or the results obtained from the use of such information

Oracle Corporation does not make any representations or warranties as to the accuracy, adequacy, or completeness of any

information contained in this Work, and is not responsible for any errors or omissions

TERMS OF USE

This is a copyrighted work and McGraw-Hill Education("McGraw-Hill)and its licensors reserve all rights in and to the

work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to

store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create

derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without

McGraw-Hills prior consent. You may use the work for your own noncommercial and personal use; any other use of the

work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms

THE WORK IS PROVIDED"AS IS. MCGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR

WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED

FROM USING THE WORK INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE

WORK VIA HYPERLINK OR OTHERWISE AND EXPRESSLY DISCLAIM ANY WARRANTY EXPRESS OR

IMPLIED INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS

FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained

in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor

its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or

for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed

through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental

special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of

them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause

whatsoever whether such claim or cause arises in contract tort or otherwise

e Book 719-6cr_pg indd 1

8/8/134:33PM

ORACLE FL/ Java Web Socket Programming Danny Coward/719-6/FM

be

Is boo

k is dedicated to bill, Jared and alex

00-FM indd

8/7/135:59PM

ORACLE FL Java Web Socket Programming /Danny Coward/719-6/FM

This page has been intentionally left blank

00-FM indd 6

8/7/135:59PM

ORACLE FL / Java Web Socket Programming/Danny Coward /719-6/FM

Contents at a Glance

1 Java Web Socket Fundamentals

2 Java Web Socket Lifecycle

23

3 Basic messaging

47

4 Configurations and Sessions

85

5 Advanced Messaging

.,。115

6 Web Socket Path Mapping∴…∴

.,。143

7 Securing Web Socket Server Endpoints

,171

8 Web Sockets in the Java EE Platform

197

Index

.,。221

00-FM indd 7

8/7/135:59PM

ORACLE FL Java Web Socket Programming /Danny Coward/719-6/FM

This page has been intentionally left blank

00-FM indd 8

8/7/135:59PM

ORACLE FL / Java Web Socket Programming/Danny Coward /719-6/FM

Contents

Acknowledgments

Introduction

Java Web Socket Fundamentals

Creating Your First Web Socket application

Creating a Web Socket Endpoint

Deploying the endpoint

Creating a Web Socket Client

23558

Web Socket Endpoints

grammatic Endpoints

Fundamental Java Web Socket API Objects

Inside the Echo Samples

15

Deployment phase

15

Accepting the First Connection

16

Web Socket Messaging

19

Summary

2 Java Web Socket Lifecycle

23

The Web Socket Protoco

24

Lifecycle of a Java Web Socket

25

The Web Socket Lifecycle in the java Web socket aPl

....27

Annotated Endpoint Event Handling

28

Lifecycle Sample

35

Programmatic Endpoint Lifecycle

41

Number of Instances and Threadin

.44

Summary

45

X

00-FM indd 9

8/7/135:59PM

【实例截图】

【核心代码】

要使用Java WebSocket和SSL WebSocket与HTTPS连接,你需要做以下几步: 1. 生成SSL证书 你需要为你的服务器生成一个SSL证书来启用SSL。你可以使用Java的keytool工具来生成自签名的SSL证书: ``` keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -validity 3650 ``` 2. 配置SSL连接 你需要在你的服务器端点配置SSL连接。这是一个示例代码,你可以根据你的需要进行修改: ``` SSLContext sslContext = SSLContext.getInstance("TLS"); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File("keystore.jks")), "password".toCharArray()); keyManagerFactory.init(keyStore, "password".toCharArray()); sslContext.init(keyManagerFactory.getKeyManagers(), null, null); HttpServer server = HttpServer.create(new InetSocketAddress(443), 0); server.setHttpsConfigurator(new HttpsConfigurator(sslContext) { public void configure(HttpsParameters params) { SSLContext context = getSSLContext(); SSLEngine engine = context.createSSLEngine(); params.setNeedClientAuth(false); params.setCipherSuites(engine.getEnabledCipherSuites()); params.setProtocols(engine.getEnabledProtocols()); params.setSSLParameters(context.getDefaultSSLParameters()); } }); ``` 3. 创建SSL WebSocket服务器 你可以使用Jetty或Tyrus等Java WebSocket库来创建SSL WebSocket服务器。这里以Tyrus为例: ``` SSLContext sslContext = SSLContext.getInstance("TLS"); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File("keystore.jks")), "password".toCharArray()); keyManagerFactory.init(keyStore, "password".toCharArray()); sslContext.init(keyManagerFactory.getKeyManagers(), null, null); Server server = new Server("localhost", 443, "/", null, ChatEndpoint.class, new HashMap<String, Object>(), sslContext); server.start(); ``` 这些步骤应该可以帮助你配置Java WebSocket和SSL WebSocket与HTTPS连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值