AngularJS: chat application with socket.io over https

I am running this chat app https://github.com/btford/angular-socket-io-im which uses socket.io/angular/node to create a basic client interface.

However, I encountered difficulties when trying to get it to work through https.

No socket events get to the server, so chat messages are not sent to clients, and users cannot join rooms. I also get this error on the client in socket.io.js :

 Uncaught TypeError: Cannot call method 'onClose' of null 

I created an express https server listening on port 8000 and changed the socket definition to:

  var socket = io.connect('https://localhost:8000',{secure: true, port:8000}); 

both in js/services.js and in /bower_components/angular-socket-io/socket.js

Not quite sure how to fix this. Thanks in advance!

+6
source share
2 answers

I have an application that does it the same way :) uses the io socket and uses: 8080 you will need to make sure your security certificate registers both https://localhost and https://localhost:8000 and has been added to your keychain otherwise the page will load, but your socket connections will fail.

0
source

It only took a few changes to make it accessible via https, although this is an old app with an expression of 2.5, which you should consider: https://github.com/guille/chat-example.git

 /** * Module dependencies. */ var fs = require('fs'); var options = { key:fs.readFileSync('key.pem'), cert:fs.readFileSync('cert.pem') }; var express = require('express'), routes = require('./routes'), socket = require('./routes/socket.js'); var app = module.exports = express.createServer(options); // Hook Socket.io into Express var io = require('socket.io').listen(app); // Configuration app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.set('view options', { layout: false }); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.static(__dirname + '/public')); app.use(app.router); }); app.configure('development', function(){ app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function(){ app.use(express.errorHandler()); }); // Routes app.get('/', routes.index); app.get('/partials/:name', routes.partials); // redirect all others to the index (HTML5 history) app.get('*', routes.index); // Socket.io Communication io.sockets.on('connection', socket); // Start server app.listen(8080, function(){ console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); }); 
0
source

Source: https://habr.com/ru/post/953116/


All Articles