drewzboto / grunt-connect-proxy

Grunt Connect support for proxying API calls during development
MIT License
424 stars 122 forks source link

Proxy to websocket doesn't seem to work #93

Open andhapp opened 9 years ago

andhapp commented 9 years ago

I am quite new to grunt and node, so please let me know anything obvious that I overlooked. I have a pretty simple set-up as I just wanted to test the proxy to a websocket server, here's the code:

WebSocket server (server.js)

var ws = require("nodejs-websocket")

var server = ws.createServer(function (conn) {
    console.log("New connection")
    conn.on("text", function (str) {
        console.log("Received "+str)
        conn.sendText(str.toUpperCase()+"!!!")
    })
    conn.on("close", function (code, reason) {
        console.log("Connection closed")
    })
}).listen(8001)

Package.json

{
  "name": "stackoverflow",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nodejs-websocket": "*",
    "grunt": "^0.4.5",
    "node-http-proxy": "*"
  },
  "devDependencies": {
    "grunt-connect-proxy": "^0.1.11",
    "grunt-contrib-connect": "^0.9.0",
    "grunt-contrib-watch": "^0.6.1"
  }
}

Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
      connect: {
          server: {
              options: {
                  keepalive: true,
                  port: 9000,
                  hostname: 'localhost'
              },
              proxies: [
                  {
                      context: '/',
                      host: 'localhost',
                      port: 8001,
                      ws: true
                  }
              ]
          }
      }
  })

  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-connect-proxy');

  grunt.registerTask('default', ['connect']);
};

All these files are in the same directory. In order to run these files, I do the following:

  1. In Terminal 1, I run the WebSocket server like: node server.js. This starts a websocket server on port 8001.
  2. In Terminal 2, I run the grunt connect, like: grunt connec:server:keepalive. This starts a websocket server on port 9000.
  3. In Chrome devtools, I try and run the following code for connecting to WebSocket:
var ws = new WebSocket('ws://localhost:9000') 

only to see the following error:

WebSocket connection to 'ws://localhost:9000/' failed: Connection closed before receiving a handshake response

Connecting directly to the websocket server works fine.

Any pointers will be appreciated!

Thanks.

Gauravkr commented 9 years ago

I am also experiencing the same issue My proxy setting : { "host": "host.abc.com", "port": 9001, "context": "/websocket", "ws": true, "xforward": false, "changeOrigin": true }

Usage : var ws = new WebSocket('ws://localhost:8000/websocket?' + id);

Am I doing any thing wrong here , or is this a reported issue ? Connecting directly to the web socket server works fine.

nvcken commented 9 years ago

+1 I am the same here My client use new SockJS('/io') can not connect by websocket on firefox, but work fine on chrome Below my grunt

{
                context: '/io',
                host: 'localhost',
                port: '5000',
                ws:true,
                changeOrigin: true,
                https: false,
                xforward: false
}
nvcken commented 9 years ago

It works - 0.2.0

andhapp commented 9 years ago

@nvcken Does it work for you on version 0.2.0?

nvcken commented 9 years ago

yes

andhapp commented 9 years ago

@nvcken I tried it with 0.2.0 and I still see the same issue. Could you use the code that I provided in the issue description and see if you could make it work locally? Cheers.

zkendall commented 6 years ago

Mine worked after I added changeOrigin: true. Apparently my server was snipping the connection during handshake.