Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9662095
commit b00c1e4
Showing
11 changed files
with
166 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
WebSockets and Java EE | ||
====================== | ||
|
||
This project is just a prototype/proposal for how WebSocket based applications could be standardized | ||
inside a Java EE container. It is by no means exhaustive nor complete. What I hope to show here is | ||
how I've been envisioning this working over the last two years I've spent implementing the Grizzly | ||
and GlassFish support. It is heavily influenced by the API I've built, of course. That said, for | ||
me this is a starting point not a final recommendation. I do feel that it's a better basis than | ||
binding any WebSocket JSR to the Servlet specification directly. For more details, please see my | ||
blog at http://antwerkz.com/websockets-and-java-ee/ |
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
56 changes: 56 additions & 0 deletions
56
websockets/src/main/java/org/jsr/websockets/DefaultWebSocket.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,56 @@ | ||
package org.jsr.websockets; | ||
|
||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
|
||
public class DefaultWebSocket implements WebSocket { | ||
public DefaultWebSocket(ServletRequest request, ServletResponse response) { | ||
} | ||
|
||
@Override | ||
public void send(String data, boolean last) { | ||
} | ||
|
||
@Override | ||
public void send(byte[] data, boolean last) { | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@Override | ||
public void close(int code, String reason) { | ||
} | ||
|
||
@Override | ||
public boolean isConnected() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean add(WebSocketListener listener) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean remove(WebSocketListener listener) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void onConnect() { | ||
} | ||
|
||
@Override | ||
public void onMessage(String text, boolean last) { | ||
} | ||
|
||
@Override | ||
public void onMessage(byte[] data, boolean last) { | ||
} | ||
|
||
@Override | ||
public void onClose(int code, String reason) { | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
websockets/src/main/java/org/jsr/websockets/DefaultWebSocketContext.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
websockets/src/main/java/org/jsr/websockets/SampleWebSocketContext.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,33 @@ | ||
package org.jsr.websockets; | ||
|
||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
|
||
import org.jsr.servlet.HttpUpgradeHandler; | ||
|
||
@HttpUpgradeHandler(protocol ="websocket", urlPatterns={"/sample"}) | ||
public class SampleWebSocketContext extends WebSocketContext { | ||
@Override | ||
public void customizeHandshake(ServletRequest request, ServletResponse response) { | ||
super.customizeHandshake(request, response); | ||
// insert cookies here | ||
} | ||
|
||
@Override | ||
public WebSocket createContext(ServletRequest request, ServletResponse response) { | ||
return new SampleWebSocket(request, response); | ||
} | ||
|
||
private static class SampleWebSocket extends DefaultWebSocket { | ||
private String userName; | ||
|
||
public SampleWebSocket(ServletRequest request, ServletResponse response) { | ||
super(request, response); | ||
userName = request.getParameter("user"); | ||
} | ||
|
||
public String getUserName() { | ||
return userName; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
websockets/src/main/java/org/jsr/websockets/WebSocket.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,25 @@ | ||
package org.jsr.websockets; | ||
|
||
public interface WebSocket { | ||
void send(String data, boolean last); | ||
|
||
void send(byte[] data, boolean last); | ||
|
||
void close(); | ||
|
||
void close(int code, String reason); | ||
|
||
boolean isConnected(); | ||
|
||
boolean add(WebSocketListener listener); | ||
|
||
boolean remove(WebSocketListener listener); | ||
|
||
void onConnect(); | ||
|
||
void onMessage(String text, boolean last); | ||
|
||
void onMessage(byte[] data, boolean last); | ||
|
||
void onClose(int code, String reason); | ||
} |
36 changes: 28 additions & 8 deletions
36
websockets/src/main/java/org/jsr/websockets/WebSocketContext.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 |
---|---|---|
@@ -1,17 +1,37 @@ | ||
package org.jsr.websockets; | ||
|
||
public interface WebSocketContext extends WebSocketListener { | ||
void send(String data, boolean last); | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
|
||
void send(byte[] data, boolean last); | ||
import org.jsr.servlet.UpgradeException; | ||
import org.jsr.servlet.UpgradeHandler; | ||
|
||
void close(); | ||
public abstract class WebSocketContext implements UpgradeHandler, WebSocketListener { | ||
@Override | ||
public final void doUpgrade(ServletRequest request, ServletResponse response) throws UpgradeException { | ||
// websocket handshake logic done here | ||
} | ||
|
||
void close(int code, String reason); | ||
public void customizeHandshake(ServletRequest request, ServletResponse response) { | ||
} | ||
|
||
boolean isConnected(); | ||
public WebSocket createContext(ServletRequest request, ServletResponse response) { | ||
return new DefaultWebSocket(request, response); | ||
} | ||
|
||
boolean add(WebSocketListener listener); | ||
@Override | ||
public void onConnect(WebSocket socket) { | ||
} | ||
|
||
boolean remove(WebSocketListener listener); | ||
@Override | ||
public void onMessage(WebSocket socket, String text, boolean last) { | ||
} | ||
|
||
@Override | ||
public void onMessage(WebSocket socket, byte[] data, boolean last) { | ||
} | ||
|
||
@Override | ||
public void onClose(WebSocket socket, int code, String reason) { | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
websockets/src/main/java/org/jsr/websockets/WebSocketListener.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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package org.jsr.websockets; | ||
|
||
public interface WebSocketListener { | ||
void onConnect(); | ||
void onConnect(WebSocket socket); | ||
|
||
void onMessage(String text, boolean last); | ||
void onMessage(WebSocket socket, String text, boolean last); | ||
|
||
void onMessage(byte[] data, boolean last); | ||
void onMessage(WebSocket socket, byte[] data, boolean last); | ||
|
||
void onClose(int code, String reason); | ||
void onClose(WebSocket socket, int code, String reason); | ||
} |
23 changes: 0 additions & 23 deletions
23
websockets/src/main/java/org/jsr/websockets/WebSocketUpgradeHandler.java
This file was deleted.
Oops, something went wrong.