-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #866 from haruntuncay/master
Add PerMessageDeflate Extension support, see #574
- Loading branch information
Showing
6 changed files
with
509 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import org.java_websocket.WebSocket; | ||
import org.java_websocket.client.WebSocketClient; | ||
import org.java_websocket.drafts.Draft; | ||
import org.java_websocket.drafts.Draft_6455; | ||
import org.java_websocket.extensions.permessage_deflate.PerMessageDeflateExtension; | ||
import org.java_websocket.handshake.ClientHandshake; | ||
import org.java_websocket.handshake.ServerHandshake; | ||
import org.java_websocket.server.WebSocketServer; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Collections; | ||
|
||
/** | ||
* This class only serves the purpose of showing how to enable PerMessageDeflateExtension for both server and client sockets.<br> | ||
* Extensions are required to be registered in | ||
* @see Draft objects and both | ||
* @see WebSocketClient and | ||
* @see WebSocketServer accept a | ||
* @see Draft object in their constructors. | ||
* This example shows how to achieve it for both server and client sockets. | ||
* Once the connection has been established, PerMessageDeflateExtension will be enabled | ||
* and any messages (binary or text) will be compressed/decompressed automatically.<br> | ||
* Since no additional code is required when sending or receiving messages, this example skips those parts. | ||
*/ | ||
public class PerMessageDeflateExample { | ||
|
||
private static final Draft perMessageDeflateDraft = new Draft_6455(new PerMessageDeflateExtension()); | ||
private static final int PORT = 8887; | ||
|
||
private static class DeflateClient extends WebSocketClient { | ||
|
||
public DeflateClient() throws URISyntaxException { | ||
super(new URI("ws://localhost:" + PORT), perMessageDeflateDraft); | ||
} | ||
|
||
@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) { } | ||
} | ||
|
||
private static class DeflateServer extends WebSocketServer { | ||
|
||
public DeflateServer() { | ||
super(new InetSocketAddress(PORT), Collections.singletonList(perMessageDeflateDraft)); | ||
} | ||
|
||
@Override | ||
public void onOpen(WebSocket conn, ClientHandshake handshake) { } | ||
|
||
@Override | ||
public void onClose(WebSocket conn, int code, String reason, boolean remote) { } | ||
|
||
@Override | ||
public void onMessage(WebSocket conn, String message) { } | ||
|
||
@Override | ||
public void onError(WebSocket conn, Exception ex) { } | ||
|
||
@Override | ||
public void onStart() { } | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
src/main/java/org/java_websocket/extensions/ExtensionRequestData.java
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,52 @@ | ||
package org.java_websocket.extensions; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public class ExtensionRequestData { | ||
|
||
public static String EMPTY_VALUE = ""; | ||
|
||
private Map<String, String> extensionParameters; | ||
private String extensionName; | ||
|
||
private ExtensionRequestData() { | ||
extensionParameters = new LinkedHashMap<String, String>(); | ||
} | ||
|
||
public static ExtensionRequestData parseExtensionRequest(String extensionRequest) { | ||
ExtensionRequestData extensionData = new ExtensionRequestData(); | ||
String[] parts = extensionRequest.split(";"); | ||
extensionData.extensionName = parts[0].trim(); | ||
|
||
for(int i = 1; i < parts.length; i++) { | ||
String[] keyValue = parts[i].split("="); | ||
String value = EMPTY_VALUE; | ||
|
||
// Some parameters don't take a value. For those that do, parse the value. | ||
if(keyValue.length > 1) { | ||
String tempValue = keyValue[1].trim(); | ||
|
||
// If the value is wrapped in quotes, just get the data between them. | ||
if((tempValue.startsWith("\"") && tempValue.endsWith("\"")) | ||
|| (tempValue.startsWith("'") && tempValue.endsWith("'")) | ||
&& tempValue.length() > 2) | ||
tempValue = tempValue.substring(1, tempValue.length() - 1); | ||
|
||
value = tempValue; | ||
} | ||
|
||
extensionData.extensionParameters.put(keyValue[0].trim(), value); | ||
} | ||
|
||
return extensionData; | ||
} | ||
|
||
public String getExtensionName() { | ||
return extensionName; | ||
} | ||
|
||
public Map<String, String> getExtensionParameters() { | ||
return extensionParameters; | ||
} | ||
} |
Oops, something went wrong.