-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
3 changed files
with
87 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import java.io.IOException; | ||
|
||
import javax.websocket.OnClose; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.OnOpen; | ||
import javax.websocket.Session; | ||
import javax.websocket.server.ServerEndpoint; | ||
|
||
/** | ||
* @ServerEndpoint gives the relative name for the end point | ||
* This will be accessed via ws://localhost:8080/EchoChamber/echo | ||
* Where "localhost" is the address of the host, | ||
* "EchoChamber" is the name of the package | ||
* and "echo" is the address to access this class from the server | ||
*/ | ||
@ServerEndpoint("/echo") | ||
public class EchoServer { | ||
/** | ||
* @OnOpen allows us to intercept the creation of a new session. | ||
* The session class allows us to send data to the user. | ||
* In the method onOpen, we'll let the user know that the handshake was | ||
* successful. | ||
*/ | ||
@OnOpen | ||
public void onOpen(Session session){ | ||
System.out.println(session.getId() + " has opened a connection"); | ||
try { | ||
session.getBasicRemote().sendText("Connection Established"); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* When a user sends a message to the server, this method will intercept the message | ||
* and allow us to react to it. For now the message is read as a String. | ||
*/ | ||
@OnMessage | ||
public void onMessage(String message, Session session){ | ||
System.out.println("Message from " + session.getId() + ": " + message); | ||
try { | ||
session.getBasicRemote().sendText(message); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* The user closes the connection. | ||
* | ||
* Note: you can't send messages to the client from this method | ||
*/ | ||
@OnClose | ||
public void onClose(Session session){ | ||
System.out.println("Session " +session.getId()+" has ended"); | ||
} | ||
} |
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,23 @@ | ||
import tornado.httpserver | ||
import tornado.websocket | ||
import tornado.ioloop | ||
import tornado.web | ||
|
||
class WSHandler(tornado.websocket.WebSocketHandler): | ||
|
||
def open(self): | ||
print 'user is connected.\n' | ||
|
||
def on_message(self, message): | ||
print 'received message: %s\n' %message | ||
self.write_message(message + ' OK') | ||
|
||
def on_close(self): | ||
print 'connection closed\n' | ||
|
||
application = tornado.web.Application([(r'/ws', WSHandler),]) | ||
|
||
if __name__ == "__main__": | ||
http_server = tornado.httpserver.HTTPServer(application) | ||
http_server.listen(5555) | ||
tornado.ioloop.IOLoop.instance().start() |