Erlen
Erlen

Reputation: 121

Node JS - Socket.io-client Not Working Well

This code works perfectly by running in the browser. However I'm trying to make it work on Node JS without success.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>

<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
  <script>
    var url = 'wss://*********.com/';
    var socket = io(url, {
      transport: ['websocket']
    });

    console.log('iniciado');

    socket.on('connect', function() {
      console.log('connected');
      socket.emit('checkout-load', {
        shopperSessionCode: "0971852c97c964e6c01ccfa7c1b131c01550675960125",
        checkoutMode: "10",
        locale: "pt_BR",
        timezone: "America/Sao_Paulo",
        cookie: "",
        forceShowBillet: 0,
        forceHideBillet: 0,
        forceHidePayPal: 0,
        forceHideTransfer: 0,
        forceHideTrial: 0,
        forceHideMultipleCards: 0,
        forceHideSamsungPay: 0,
        defaultInstallmentOption: 0,
        thumb: 0,
        checkoutCustomId: null,
        templateLayoutId: null,
        hotAffiliateCookiesInfo: {
          urlDomain: ".*****.com",
          hotAffiliateCookieVO: [{
            name: "hotd",
            value: "",
            domain: ".hotmart.com"
          }]
        },
        urlCheckout: "https://*******",
        previousUrl: "https://********",
        callbackEvent: "checkoutLoaded",
        affiliationReference: [{
          amount: 1,
          affiliationReference: "******"
        }]
      });
    });
    socket.on('checkoutLoaded', function(data) {
      console.log('evento recebido');
      console.log(data);
    });
    socket.on('disconnect', function() {
      console.log('disconnect');
    });
    socket.on('ping', function() {
      console.log('ping');
    });
    socket.on('pong', function() {
      console.log('pong');
    });

    console.log('finalizado');
  </script>
</body>

</html>

The code connects to the websocket, however the socket.emit ('...') command is not sent correctly after the connection. Also tested using socket.send () and also does not work.

Node JS code:

var socket = require('socket.io-client')('wss://*******/',{transports: ['websocket']});

console.log('iniciado');

socket.on('connect', function(data){
    console.log('connected');

    socket.emit('checkout-load', {
        shopperSessionCode:"0971852c97c964e6c01ccfa7c1b131c01550675960125",
        checkoutMode:"10",
        locale:"pt_BR",
        timezone:"America/Sao_Paulo",
        cookie:'',
        forceShowBillet:0,
        forceHideBillet:0,
        forceHidePayPal:0,
        forceHideTransfer:0,
        forceHideTrial:0,
        forceHideMultipleCards:0,
        forceHideSamsungPay:0,
        defaultInstallmentOption:0,
        thumb:0,
        checkoutCustomId:null,
        templateLayoutId:null,
        hotAffiliateCookiesInfo:{
            urlDomain:".hotmart.com",
            hotAffiliateCookieVO:[
                {
                    name:"hotd",
                    value:"******",
                    domain:".hotmart.com"
                }
            ]
        },
        urlCheckout:"https://********",
        previousUrl:"https://************",
        callbackEvent:"checkoutLoaded",
        affiliationReference:[
            {
                amount:1,
                affiliationReference:"*****"
            }
        ]
    });
});

socket.on("checkoutLoaded", function(data){
    console.log('evento recebido: checkoutLoaded');
    console.log(data);
});

Can someone help me?

Upvotes: 0

Views: 98

Answers (1)

amrutk
amrutk

Reputation: 36

Please check socket.io documentation.It is clearly mentioned that it is not websocket implemention.

https://socket.io/docs/#What-Socket-IO-is-not

Upvotes: 1

Related Questions