Answer the question
In order to leave comments, you need to log in
node.js segmentation error how to fix?
Good day, I am doing a chat on node.js and socket.io wrote the server code
var options = {
// 'log level': 0
};
var fs = require('fs'),
express = require('express'),
app = express(),
socketio = require('socket.io'),
config = require('./config');
var serverPort = config.port || 9876, // Listen port
secure = config.secure || false; // use HTTPS/SSL
var app = express();
if (secure)
{
var options = {
key: fs.readFileSync(config.secure_key),
cert: fs.readFileSync(config.secure_cert)
};
var server = require('https').createServer(options, app);
} else
{
var server = require('http').createServer(app);
}
server.listen(serverPort, function() {
var addr = server.address();
console.log(' app listening on ' + (secure ? 'https://' : 'http://') + addr.address + ':' + addr.port);
});
var io = require('socket.io').listen(server, options);
app.use('/static', express.static(__dirname + '/static'));
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (client) {
client.on('message', function (message) {
try {
client.emit('message', message);
client.broadcast.emit('message', message);
} catch (e) {
console.log(e);
client.disconnect();
}
});
});
{
"port": "9876",
"secure": true,
"secure_key" : "/opt/privkey.pem",
"secure_cert" : "/opt/fullchain.pem"
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question