习WebSocket一(WebSocket初识)
http://www.cnblogs.com/wgp13x/p/3812579.html
Java EE 7 去年刚刚发布了JSR356规范,使得WebSocket的Java API得到了统一,Tomcat从7.0.47开始支持JSR356,这样一来写WebSocket的时候,所用的代码都是可以一样的。今天终于体验了一把Tomcat发布的WebSocket,用着很爽,下面把这一历程分享给大家。
界面效果? | 服务端代码 |
打开一个页面,首先点击Connect,保证连接到Websocket,
再在输入框里输入"I am angel1!",
点击Echo message,可以看到下面框里输入Sent和Received信息。
可以看出,在OnMessage处就是简单的通过传来的Session得到某一客户端, 再向其发出同样的消息。 |
@ServerEndpoint("/websocket/echoAnnotation")
public class EchoAnnotation {
@OnMessage
public void echoTextMessage(Session session, String msg, boolean last) {
try {
if (session.isOpen()) {
session.getBasicRemote().sendText(msg, last);
}
} catch (IOException e) {
try {
session.close();
} catch (IOException e1) {
// Ignore
}
}
}
@OnMessage
public void echoBinaryMessage(Session session, ByteBuffer bb,
boolean last) {
try {
if (session.isOpen()) {
session.getBasicRemote().sendBinary(bb, last);
}
} catch (IOException e) {
try {
session.close();
} catch (IOException e1) {
// Ignore
}
}
}} |
2、Chat
界面效果? | 服务端代码 |
打开第一个页面,它会告诉你,你已经加入聊天了。 分析代码,就是一个新连接,会自动实例化一个ChatAnnotation, 这些ChatAnnotation对象共用同一些属性, 最重要的就是Set<ChatAnnotation> conncetions, 在OnOpen处把自身实例加入到conncetions中,并广播消息。 广播消息,是轮循conncetions并发送消息。 在界面输入对话框处输入文字,回车,消息就会发送到服务端。 就会传入到服务端某ChatAnnotation的OnMessage处, 然后把收到的消息与自身名称拼接后,再广播出去。 这下在线的客户端就都能够收到消息了。 第一个页面: 第二个页面: |
@ServerEndpoint(value = "/websocket/chat")
public class ChatAnnotation {
private static final String GUEST_PREFIX = "Guest";
private static final AtomicInteger connectionIds = new AtomicInteger(0);
private static final Set<ChatAnnotation> connections =
new CopyOnWriteArraySet<ChatAnnotation>();
private final String nickname;
private Session session;
public ChatAnnotation() {
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
}
@OnOpen
public void start(Session session) {
this.session = session;
connections.add(this);
String message = String.format("* %s %s", nickname, "has joined.");
broadcast(message);
}
@OnClose
public void end() {
connections.remove(this);
String message = String.format("* %s %s",
nickname, "has disconnected.");
broadcast(message);
}
@OnMessage
public void incoming(String message) {
// Never trust the client
String filteredMessage = String.format("%s: %s",
nickname, HTMLFilter.filter(message.toString()));
broadcast(filteredMessage);
}
private static void broadcast(String msg) {
for (ChatAnnotation client : connections) {
try {
client.session.getBasicRemote().sendText(msg);
} catch (IOException e) {
connections.remove(client);
try {
client.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
client.nickname, "has been disconnected.");
broadcast(message);
}
}
}
} |
@ServerEndpoint(value = "/websocket/snake")
public class SnakeAnnotation {
public static final int PLAYFIELD_WIDTH = 640;
public static final int PLAYFIELD_HEIGHT = 480;
public static final int GRID_SIZE = 10;
private static final AtomicInteger snakeIds = new AtomicInteger(0);
private static final Random random = new Random();
private final int id;
private Snake snake;
|
public static String getRandomHexColor() {
float hue = random.nextFloat();
// sat between 0.1 and 0.3
float saturation = (random.nextInt(2000) + 1000) / 10000f;
float luminance = 0.9f;
Color color = Color.getHSBColor(hue, saturation, luminance);
return ‘#‘ + Integer.toHexString(
(color.getRGB() & 0xffffff) | 0x1000000).substring(1);
}
public static Location getRandomLocation() {
int x = roundByGridSize(random.nextInt(PLAYFIELD_WIDTH));
int y = roundByGridSize(random.nextInt(PLAYFIELD_HEIGHT));
return new Location(x, y);
}
private static int roundByGridSize(int value) {
value = value + (GRID_SIZE / 2);
value = value / GRID_SIZE;
value = value * GRID_SIZE;
return value;
}
public SnakeAnnotation() {
this.id = snakeIds.getAndIncrement();
}
|
@OnOpen
public void onOpen(Session session) {
this.snake = new Snake(id, session);
SnakeTimer.addSnake(snake);
StringBuilder sb = new StringBuilder();
for (Iterator<Snake> iterator = SnakeTimer.getSnakes().iterator();
iterator.hasNext();) {
Snake snake = iterator.next();
sb.append(String.format("{id: %d, color: ‘%s‘}",
Integer.valueOf(snake.getId()), snake.getHexColor()));
if (iterator.hasNext()) {
sb.append(‘,‘);
}
}
SnakeTimer.broadcast(String.format("{‘type‘: ‘join‘,‘data‘:[%s]}",
sb.toString()));
}
@OnMessage
public void onTextMessage(String message) {
if ("west".equals(message)) {
snake.setDirection(Direction.WEST);
} else if ("north".equals(message)) {
snake.setDirection(Direction.NORTH);
} else if ("east".equals(message)) {
snake.setDirection(Direction.EAST);
} else if ("south".equals(message)) {
snake.setDirection(Direction.SOUTH);
}
} |
@OnClose
public void onClose() {
SnakeTimer.removeSnake(snake);
SnakeTimer.broadcast(String.format("{‘type‘: ‘leave‘, ‘id‘: %d}",
Integer.valueOf(id)));
}
@OnError
public void onError(Throwable t) throws Throwable {
// Most likely cause is a user closing their browser. Check to see if
// the root cause is EOF and if it is ignore it.
// Protect against infinite loops.
int count = 0;
Throwable root = t;
while (root.getCause() != null && count < 20) {
root = root.getCause();
count ++;
}
if (root instanceof EOFException) {
// Assume this is triggered by the user closing their browser and
// ignore it.
} else {
throw t;
}
}} |
界面和ClientEndpoit? |
入口代码
|
下面是调用了echoAnnotation的websocket的客户端与服务端交互过程。
同样是客户端发给服务端一个消息,服务端收到后发给客户端,
客户端收到后显示出来。
客户端代码也很简单,没有什么逻辑,只管把接收的打印出来就行了。
需要注意的是,需要引用的jar包只在Java EE 7中包含。
包括javax.websocket-api.jar、tyrus-client.jar、
tyrus-container-grizzly.jar、tyrus-core.jar、
tyrus-websocket-core.jar、tyrus-spi.jar、tyrus-server.jar、
nucleus-grizzly-all.jar
同样的也可以调用其它的websocket,比如chat...使用起来非常方便。
@ClientEndpoint
public class MyClient {
@OnOpen
public void onOpen(Session session) {
}
@OnMessage
public void onMessage(String message) {
System.out.println("Client onMessage: " + message);
}
@OnClose
public void onClose() {
}
|
public class Main {
private static String uri = "ws://localhost/examples/websocket/echoAnnotation";
private static Session session;
private void start() {
WebSocketContainer container = null;
try {
container = ContainerProvider.getWebSocketContainer();
} catch (Exception ex) {
System.out.println("error" + ex);
}
try {
URI r = URI.create(uri);
session = container.connectToServer(MyClient.class, r);
} catch (DeploymentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Main client = new Main();
client.start();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
try {
do {
input = br.readLine();
if (!input.equals("exit"))
client.session.getBasicRemote().sendText(input);
} while (!input.equals("exit"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。