Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can socket.io-client use https? #1233

Closed
ikuratakashi opened this issue Sep 16, 2018 · 10 comments
Closed

Can socket.io-client use https? #1233

ikuratakashi opened this issue Sep 16, 2018 · 10 comments

Comments

@ikuratakashi
Copy link

Can socket.io-client use https?

connect does not run.

Is option missing?

//設定ファイルの読み込み
require('dotenv').config();

var fs = require('fs');
var ssl_server_key = '../../ssl/new_server.key';
var ssl_server_crt = '../../ssl/server.crt';
var options = {
    key: fs.readFileSync(ssl_server_key),
    cert: fs.readFileSync(ssl_server_crt),
    pfx: fs.readFileSync(ssl_server_crt),
    passphrase: process.env.HTTPS_PASS
};

//socket.io-clientでサーバへ接続
var client = require('socket.io-client');
var socket = client.connect('https://localhost:30000/namespace',options);

//処理
//connectしたら'how are you?'とメッセージを送信する
socket.on('connect',function(){
    console.log('yea!!');
    socket.send('how are you?');
    socket.disconnect();
    process.exit(0);
});
@atefBB
Copy link

atefBB commented Oct 8, 2018

Hi @ikuratakashi !
I have the same prob, how did you solve this?

@ikuratakashi
Copy link
Author

//---------------------------------------------

// socket.io-client クラス定義

//---------------------------------------------

naoetu.clsClient = function(){return this.initialize.apply(this,arguments);};

naoetu.clsClient.prototype = {

//引数1 pUrl:接続するURL(例:https://hotge.com:99999/namespace)

initialize : function(pUrl){

    this.url = pUrl;

    this.client = require('socket.io-client');

    this.options = {

        secure:true,

        reconnect: true,

        rejectUnauthorized : false

    };

    this.isConnect = false;

    this.socket = false;

},

//コネクション実行

DoConnect : function(){

    this.socket = this.client.connect(this.url,this.options);

    //接続完了時

    this.socket.on('connect',naoetu.bind(this,function(){

        //thisはsocketの空間になるので注意

        naoetu.log.out(3,'clsClient: connect finish id:' + this.socket.id);

        this.isConnect = true;

    }));

    //接続エラー時

    this.socket.on('connect_error',function(e){

        //thisはsocketの空間になるので注意

        naoetu.log.out(3,'clsClient: connect error :'+ e);

        this.isConnect = false;

    });

    //接続解除時

    this.socket.on('disconnect',function(pRes){

        //thisはsocketの空間になるので注意

        naoetu.log.out(3,'clsClient: disconnect :'+ pRes);

        this.isConnect = false;

    });

}

}

var client = new naoetu.clsClient("https://exmple.com:99999/namespace");
client.DoConnect();
client.socket.emit('message',{msg:"local message"});

@ikuratakashi
Copy link
Author

This is important.

options = {
secure:true,
reconnect: true,
rejectUnauthorized : false
};

Thank you.

@atefBB
Copy link

atefBB commented Oct 9, 2018

@ikuratakashi It helps! ありがとうございました!

@knoxcard
Copy link

@knoxcard
Copy link

Close ticket?

@atefBB
Copy link

atefBB commented Dec 28, 2018

@ikuratakashi Yes, you can.

@modavidc
Copy link

I don't know who are you but the only thing that I know is I love you, ikura takashi.

Hontoni arigato gozaimasu.

@darrachequesne
Copy link
Member

Example added here: https://socket.io/docs/v3/client-initialization/#Node-js-specific-options

Client

const fs = require("fs");
const socket = require("socket.io-client")("https://example.com", {
  ca: fs.readFileSync("./cert.pem")
});

Server

const fs = require("fs");
const server = require("https").createServer({
  cert: fs.readFileSync("./cert.pem"),
  key: fs.readFileSync("./key.pem")
});
const io = require("socket.io")(server);

@markosole
Copy link

Here is full example of setup with https for those who are still searching for solution / fix
rikulo/socket.io-client-dart#319

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

No branches or pull requests

6 participants