Reputation: 2105
I would like to make connection to my websocket server which i basicly need to be over https.
from tornado.options import define, options
from imaplib import Commands
define("port", default=443, help="run on the given port", type=int)
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def __init__(self, *args, **kwargs):
super(WebSocketHandler, self).__init__(*args, **kwargs);
pass;
def open(self):
print 'new connection'
self.write_message("connected")
def on_message(self, message):
print 'message received %s' % message
self.write_message('message received %s' % message)
def on_close(self):
print 'connection closed'
def check_origin(self, origin):
return True;
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
(r"/ws", WebSocketHandler)
]
)
data_dir = "/home/pi/projects/WebSocketOverHttps/";
httpServer = tornado.httpserver.HTTPServer(app, ssl_options = {
"certfile": os.path.join(data_dir, "cert.crt"),
"keyfile": os.path.join(data_dir, "key.key"),
});
httpServer.listen(options.port)
print "Listening on port:", options.port
tornado.ioloop.IOLoop.instance().start()
the problem is that my browser says: "WebSocket connection to 'wss://192.168.1.8/ws' failed: WebSocket opening handshake was canceled"
The things what i tried to do was: 1) Add certificate to system (win 8.1 PRO x64) by double clicking on cert file 2) Add certificate to google chrome on the same system (through settings of browser)
I am able to connect to this server when i reimplement it to use http instead of https so physical connection to machine looks OK.
my certificate are self-signed, generated by command:
sudo openssl req -x509 -nodes -days 365000 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
this is how i try to connect to it:
var socket = new WebSocket("wss://192.168.1.8:443/ws");
Upvotes: 1
Views: 8298
Reputation: 2105
As @BenDarnell posted, We have to accept this certificate by browse to page of this server. Then your browser will inform that this site is untrusted. Let your browser to use this untrusted certificate and that's all. Here is code you will need to place in code:
class MainHandler(tornado.web.RequestHandler):
def get(self):
loader = tornado.template.Loader(".")
self.write(loader.load("index.html").generate());
app = tornado.web.Application(
handlers=[
(r"/ws", WebSocketHandler),
(r"/", MainHandler)
])
data_dir = "/home/pi/projects/Something";
ssl_options_dict = {
"certfile": os.path.join(data_dir, "cert.crt"),
"keyfile": os.path.join(data_dir, "key.key"),
};
httpServer = tornado.httpserver.HTTPServer(app, ssl_options = ssl_options_dict);
Upvotes: 2