Skip to content

Commit

Permalink
add web socket server
Browse files Browse the repository at this point in the history
  • Loading branch information
bikash committed Feb 17, 2016
1 parent 7ad370e commit f80d474
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
12 changes: 7 additions & 5 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,17 @@
});
}
$(document).ready(function() {
var wsUri = "ws://localhost:8000/";

var socket = new WebSocket(wsUri);
var wsUri = "ws://localhost:5555/ws";
var host = "ws://localhost:5555/ws";
//var eb = new WebSocket(host);
//var host = "ws://localhost:9090/ws";

//console.log("socket status: " + socket.readyState);
hrElement = $("#hr"); //document.getElementById("hr");
heartElement = $("#heart");
setupSmootie();
eb = new EventBus(socket);
//eb = new EventBus('http://localhost:5555/eventbus');
//eb = new EventBus(socket);
eb = new EventBus("http://localhost:5555/ws");
eb.onopen = function () {
subscribeToHeartrate();
subscribeToActivity();
Expand Down
57 changes: 57 additions & 0 deletions websocket.java
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");
}
}
23 changes: 23 additions & 0 deletions websocket.py
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()

0 comments on commit f80d474

Please sign in to comment.