首页 新闻 赞助 找找看

有一个问题,关于node的问题

0
悬赏园豆:100 [待解决问题]

直接上代码

index.js

const Koa = require('koa')
const App = new Koa()
const server = require('http').createServer(App.callback())
const WebSocket = require('ws')
const ws = new WebSocket.Server({ server })
const kline = require('./data')

ws.on('connection', function connection(ws) {

  ws.on('message', function incoming(message) {

    const data = JSON.parse(message)

    if (data.type === 'kline') {
      if (data.resolution === '1D') {
        ws.send(JSON.stringify({ kline: kline, symbol: data.symbol, resolution: data.resolution, type: data.type }))
      } else {
        createRandomData(ws, data)
      }
    }

    if (data.type === 'updata') {
      if (data.resolution === '1D') {
        // 一天以及一天以上暂时不做处理
        ws.send(JSON.stringify({ ping: Date.now() }))
      } else {
        // 这里返回最新数据
        const num = setRandomValue(2, 2)
        ws.send(JSON.stringify({
          kline: [
            {
              time: (parseInt(Date.now() / 10000) + (data.resolution * 6)) * 10000,
              open: num - setRandomValue(1),
              close: num - setRandomValue(1),
              low: num - setRandomValue(1),
              high: num,
              volume: 2554477 - setRandomValue(6)
            }
          ],
          symbol: data.symbol,
          resolution: data.resolution,
          type: data.type
        }))
      }
    }
  })

})

const setRandomValue = (integerNum, decimalNum) => {

  let randomNum = Math.random() * Math.pow(10, integerNum) + Math.pow(10, integerNum)
  // 整数部分
  let integerPart = 0
  if (integerNum) {
    integerPart = Math.floor(randomNum)
  }
  //  小数部分
  let decimalPart = 0.00
  if (decimalNum) {
    decimalPart = randomNum.toString().split(".")[1].substring(0, decimalNum)
    decimalPart = '0.' + decimalPart
    decimalPart = parseFloat(decimalPart)
  }
  return integerPart + decimalPart
}

const createRandomData = (ws, data) => {
  const { symbol, resolution, from, to, type } = data
  const list = []
  for (let i = 0; i < 1000; i++) {
    const num = setRandomValue(2, 2)
    const item = {
      time: (parseInt(Date.now() / 10000) - (i * resolution * 6)) * 10000,
      open: num - setRandomValue(1),
      close: num - setRandomValue(1),
      low: num - setRandomValue(1),
      high: num,
      volume: 2554477 - setRandomValue(6)
    }
    list.push(item)
  }

  ws.send(JSON.stringify({ kline: list, symbol: data.symbol, resolution: data.resolution, type: data.type }))
}

server.listen(3010, () => {
  console.log(`Server listening at port 3010...`)
})

data.js

module.exports = [
    {
    "date": "2015-09-25 08:00:00",
    "time": 1443139200000,
    "open": 239.99,
    "close": 235.2,
    "low": 234.81,
    "high": 239.99,
    "volume": 190090
  },
  {
    "date": "2015-09-26 08:00:00",
    "time": 1443225600000,
    "open": 235.2,
    "close": 234.51,
    "low": 232.91,
    "high": 235.36,
    "volume": 258488
  }
]

我想实现index.js里面的读取内容 在const kline = require('./data')这里为为data.js里面的json数据,类似这样子的

[
  {
    "date": "2015-09-25 08:00:00",
    "time": 1443139200000,
    "open": 239.99,
    "close": 235.2,
    "low": 234.81,
    "high": 239.99,
    "volume": 190090
  },
  {
    "date": "2015-09-26 08:00:00",
    "time": 1443225600000,
    "open": 235.2,
    "close": 234.51,
    "low": 232.91,
    "high": 235.36,
    "volume": 258488
  }
]

请问如何实现呢?

问题补充:

我希望把data.js换成data.json文件  然后再在index.js里面使用ajax请求这个json,不是使用data.js  而是想使用data.json    谢谢各位,指点下呗

雨落秋垣的主页 雨落秋垣 | 初学一级 | 园豆:137
提问于:2018-07-27 17:27
< >
分享
所有回答(1)
0

类似如下

a.js:

console.log('a 开始'); exports.done = false; const b = require('./b.js'); console.log('在 a 中,b.done = %j', b.done); exports.done = true; console.log('a 结束');

b.js:

console.log('b 开始'); exports.done = false; const a = require('./a.js'); console.log('在 b 中,a.done = %j', a.done); exports.done = true; console.log('b 结束');

main.js:

console.log('main 开始'); const a = require('./a.js'); const b = require('./b.js'); console.log('在 main 中,a.done=%j,b.done=%j', a.done, b.done);

舒碧 | 园豆:169 (初学一级) | 2018-07-27 18:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册