Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Problem WebSocket #41

Open
alessioguglielmo opened this issue Mar 20, 2015 · 7 comments
Open

Problem WebSocket #41

alessioguglielmo opened this issue Mar 20, 2015 · 7 comments

Comments

@alessioguglielmo
Copy link

I wrote a plugin (and put it in ~/.c9/plugin/plugin-name) with following code:

var WebSocket = require('ws')
  , ws = new WebSocket('ws://mylocalipaddress:8090/');
ws.on('open', function() {
    ws.send('something');
});
ws.on('message', function(message) {
    console.log('received: %s', message);
});

On localhost I have a server listening on port 8090

var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({port: 8090});
wss.on('connection', function(ws) {
    ws.on('message', function(message) {
        console.log('received: %s', message);
    });
    ws.send('something');
});

When I try to load the IDE at

http://localhost:8186/ide.html?sdk=1?debug=2

It loads only IDE but not the plugin and my server doesn't receive any data.

If I try the same code in a client loaded in a different container it works perfectly.

Thanks

@javruben
Copy link

Could you paste the full code of your plugin (first to last line)?

Thanks.

On Mar 20, 2015, at 9:52 AM, alessioguglielmo notifications@github.com wrote:

I wrote a plugin (and put it in ~/.c9/plugin/plugin-name) with following code:

var WebSocket = require('ws')
, ws = new WebSocket('ws://mylocalipaddress:8090/');
ws.on('open', function() {
ws.send('something');
});
ws.on('message', function(message) {
console.log('received: %s', message);
});
On localhost I have a server listening on port 8090

var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8090});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
});
ws.send('something');
});
When I try to load the IDE at

http://localhost:8186/ide.html?sdk=1?debug=2

It loads only IDE but not the plugin and my server doesn't receive any data.

If I try the same code in a client loaded in a different container it works perfectly.

Thanks


Reply to this email directly or view it on GitHub #41.

@alessioguglielmo
Copy link
Author

define(function(require, exports, module) {
    main.consumes = [
        "Plugin", "commands", "ui", "util", "Dialog", "dialog.alert", "tabManager"
    ];
    main.provides = ["example1"];
    return main;

    function main(options, imports, register) {
          var Plugin = imports.Plugin;
          var commands = imports.commands;
          var ui = imports.ui;
          var util = imports.util;

        /***** Initialization *****/

        var plugin = new Plugin("Ajax.org", main.consumes);
        var emit = plugin.getEmitter();
        var api  = plugin.setAPIKey(options.apikey);

        var loaded = false;
        function load() {
            if (loaded) return false;
            loaded = true;                      

        var WebSocket = require('ws')
            , ws = new WebSocket('ws://172.17.2.119:8090/');
        WebSocket.on('open', function() {
            WebSocket.send('something');
        });
        WebSocket.on('message', function(message) {
            console.log('received: %s', message);
        });

            // Test use of the API
            // api.setPersistentData("user", { example: 123 }, function(err){
            //     api.getPersistentData("user", function(err, data){
            //         console.error(data.example === 123);
            //     });
            // });

            // Test loading relative content
            console.error(require("text!./test.html"));

            // Test dependencies
            console.error(commands, options.color);
        }

        /***** Methods *****/

        /***** Lifecycle *****/

        plugin.on("load", function() {
            load();
        });
        plugin.on("enable", function() {

        });
        plugin.on("disable", function() {

        });
        plugin.on("unload", function() {
            loaded = false;
        });

        /***** Register and define API *****/

        /**
         * 
         **/
        plugin.freezePublicAPI({
            _events: [
                /**
                 * @event draw
                 */
                "draw"
            ]
        });

        register("", {
            example1: plugin
        });
    }
});

@javruben
Copy link

The plugin looks good, although I don't fully understand why you set an outside IP address in your plugin.

It loads only IDE but not the plugin and my server doesn't receive any data.

How do you know the plugin isn't loaded? Could you test by setting a console.log in your load() function?

If you check the network tab of the browser's console. Do you see the websocket trying to connect?

@alessioguglielmo
Copy link
Author

I have c9 on a container and a WebSocket server in a different docker
container. I suppose that plugin isn't loaded because in firebug I can't
find trace about console.log (when I added it) and also alert messages
aren't displayed at all. Likely it found some error and blocks js
execution... I will try to re-implement the scenario and check the network
tab.

2015-03-21 19:53 GMT+01:00 Ruben Daniels notifications@github.com:

The plugin looks good, although I don't fully understand why you set an
outside IP address in your plugin.

It loads only IDE but not the plugin and my server doesn't receive any
data.
How do you know the plugin isn't loaded? Could you test by setting a
console.log in your load() function?

If you check the network tab of the browser's console. Do you see the
websocket trying to connect?


Reply to this email directly or view it on GitHub
#41 (comment).

@alessioguglielmo
Copy link
Author

Nope. In the network tab my websocket is absent...

@alessioguglielmo
Copy link
Author

I've checked package.json file. Should I add something to it?

{
  "name": "c9.ide.example",
  "description": "Cloud9 Custom Example Plugin",
  "version": "1.12.16",
  "author": "Ajax.org B.V. <info@ajax.org>",
  "contributors": [
    {
      "name": "Ruben Daniels",
      "email": "ruben@c9.io"
    }
  ],
  "repository": {
    "type": "git",
    "url": "http://github.com/javruben/example.git"
  },
  "plugins": {
    "example": {}
  },
  "categories": [
    "example"
  ],
  "licenses": []
}

I tried to use other "common node.js API" such as path or querystring (that have slightly different sintax) that I've seen in the documentation and they work perfectly. The problem is with "ws"

@alessioguglielmo
Copy link
Author

Worked around the problem using different way, passing data through fs, although the original problem I have quoted persists.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants