This repository has been archived by the owner on May 3, 2018. It is now read-only.
forked from apache/tomcat
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apparently Ant is smart enough to grab everything. Fixed a minor bug with server-initiated closing handshakes. (Eats pings.)
- Loading branch information
Showing
6 changed files
with
313 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package websocket; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import org.apache.catalina.websocket.WebSocketConnection; | ||
import org.apache.catalina.websocket.WebSocketFrame; | ||
import org.apache.catalina.websocket.WebSocketServlet; | ||
|
||
public class Chat extends WebSocketServlet { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private final List<WebSocketConnection> connections = | ||
new LinkedList<WebSocketConnection>(); | ||
|
||
@Override | ||
protected WebSocketConnection createWebSocketConnection() { | ||
return new ChatConnection(); | ||
} | ||
|
||
private final class ChatConnection extends WebSocketConnection { | ||
|
||
private static final long maxMessageSize = 65536; // 64KB | ||
|
||
@Override | ||
protected void onOpen() { | ||
connections.add(this); | ||
} | ||
|
||
@Override | ||
protected void onMessage(WebSocketFrame frame) throws IOException { | ||
|
||
// There may be some clever way to fork input streams in | ||
// a 1) single-threaded and 2) blocking environment, but I | ||
// haven't discovered it yet. For now, we'll assume that | ||
// people only send reasonable-sized chat messages. | ||
if(frame.getPayloadLength() > maxMessageSize) { | ||
System.out.println("Payload too big for a chat message"); | ||
close(); | ||
} | ||
|
||
// Pull the payload into a byte array | ||
byte[] payload = frame.getPayloadArray(); | ||
|
||
//System.out.println(Arrays.toString(payload)); | ||
|
||
for(WebSocketConnection connection : connections) { | ||
frame.setPayload(payload); | ||
connection.send(frame); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onClose() { | ||
connections.remove(this); | ||
} | ||
|
||
@Override | ||
protected void onError() { | ||
System.err.println("Chat WebSocket error"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.