通过在浏览器中运行,此代码可以完美运行。但是,我正在尝试使其无法在Node JS上运行。
<!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>
代码连接到websocket,但是在连接后未正确发送socket.emit('...')命令。还使用socket.send()进行了测试,并且也无法正常工作。
Node JS代码:
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);
});
有人可以帮我吗?
答案 0 :(得分:1)
请检查socket.io文档。显然提到这不是websocket实现。