使用套接字构建实时API

时间:2018-09-16 20:57:55

标签: html sockets

我试图制作一个带有显示股票更新的套接字的静态页面,该页面必须与此连接:https://ws-api.iextrading.com/1.0,但是它不起作用

$(window).scroll(function (event) {
    var scroll = $(window).scrollTop();
    $('andtxt').toggleClass('andtxtscroll',

        scroll >= $('.trig').offset().top
    );
});

//trigger the scroll
$(window).scroll();//ensure if you're in current position when page is refreshed

这是脚本connecton

<!doctype html>
<html>
<head>
</head>
<body>

<h1>hellow world!</h1>


<script>
// server.js
io.on('connection', (socket) => {
  socket.emit('message', `A new user, ${Date.now()}, has connected`);
});
</script>

<script>
// public/application.js
socket.on('connect', () => {
  console.log('You have connected!'); // This will log to the browser's console, not the terminal
});

</script>
<script>
// server.js
io.on('connection', (socket) => {

  socket.emit('message', `A new user, ${Date.now()}, has connected`);

  socket.on('message', (message) => {
    console.log(`The new user's name is ${message.username}, and his message is: ${message.text}`);
  });

  socket.on('disconnect', () => {
    console.log('A user has disconnected.');
  });
});
</script>

</body>
</html>

如果有人知道如何连接// Import socket.io with a connection to a channel (i.e. tops) const socket = require('socket.io-client')('https://ws-api.iextrading.com/1.0/tops') // Listen to the channel's messages socket.on('message', message => console.log(message)) // Connect to the channel socket.on('connect', () => { // Subscribe to topics (i.e. appl,fb,aig+) socket.emit('subscribe', 'snap,fb,aig+') // Unsubscribe from topics (i.e. aig+) socket.emit('unsubscribe', 'aig+') }) // Disconnect from the channel socket.on('disconnect', () => console.log('Disconnected.')) ,请告诉我。

1 个答案:

答案 0 :(得分:-1)

如果您是说使用TCP套接字,那么您可能首先了解socket connection 然后要拥有Web服务器,还必须下载Web服务器ruby