nodejs + socket.io配置
阿新 • • 發佈:2019-01-25
netstat -lnp|grep 18081
kill -9 [id] // kill -9 23056
nginx.conf(https):
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
port_in_redirect off;
server {
listen 80;
server_name demo.tijos.net;
rewrite ^(.*)$ https://$host$1 permanent;
}
# HTTPS server
#
server {
listen 443;
server_name localhost;
ssl on;
root html;
index index.html index.htm;
ssl_certificate keys/1529914169199 .pem;
ssl_certificate_key keys/1529914169199.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:18081;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
node
let bodyParser = require('body-parser');
let express = require('express');
let app= express();
let server=require('http').Server(app);
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
if(req.method == 'OPTIONS') {
//讓options請求快速返回
res.sendStatus(200);
} else {
next();
}
});
app.get('/', function (req, res) {
console.log('');
res.json({
data: 111,
message: 333
});
});
app.post('/data', function (req, res) {
console.log('hi,data');
console.log(req.body);
console.log('**************************************************');
res.send(" post successfully!");
});
io.sockets.on('connection',function(socket){
console.log('hello,friend');
socket.emit('users','1111111');
socket.broadcast.emit('users','1111111');
socket.on('disconnect',function(){
console.log('User disconnected');
socket.broadcast.emit('users','1111111');
});
});
server.listen(18081,function(){
console.log('server begin...');
});
client
var url = 'https://************.com'; //注意:應為伺服器採用https,所以url一定用https !!!!!!!!!
$.ajax({
type: "GET",
url: url,
dataType: "json",
success: function(data){
console.log(data.data);
},
error: function (err) {
console.log(err);
}
});
var socket = io.connect(url);