Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

apache - Is it possible to use port 80 for both HTTP and web socket traffic?

  1. I'm building a site that uses web sockets (technically Flash sockets) in order to provide real-time communication.
  2. I want to be able to support people behind corporate/academic firewalls that block everything except port 80
  3. I'd like to be able to run the site off of a single machine

Previously, I've been using Apache for HTTP serving combined with some python listening on a high-numbered socket for the websocket stuff, but that obviously won't work here.

I can always move the websocket stuff to a separate server, but I'd like to avoid paying for a second VPS (and have to talk to the database over the network instead of locally). Is there a good way to do this (nodejs, nginx, ..?), or is it not worth the headache?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

YES, by using node.js. Express or connect for the HTTP file serving and socket.io for the WebSocket stuff.

Example:

var express = require("express");
var app = express.createServer(); 

app.get('/', function(req, res){ 
    res.redirect("/index.html");
}); 

app.configure(function(){
  app.use(express.static(__dirname + '/public'));
});

app.listen(80); 

var io = require('socket.io'); 
var socket = io.listen(app); 
socket.on('connection', function(client){ 
  client.on('message', function(){...});
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...