我正在创建一个具有api和套接字的简单NodeJS / Express webapp。基本上,我想要做的是从外部源向我的API发送数据,然后在pageView / id中将这些数据发送到套接字。现在它可以工作,但是它将数据发送到所有视图,而不是特定的pageView / id。
我知道我必须创建会议室,我尝试通过让我的套接字加入房间,当webapp导航到页面/ id时:(注意我没有登录用户)
Warning message:
In match.names(columns, colnames(data)) :
all columns not located in Close SMA for SPY.Open SPY.High SPY.Low SPY.Close
SPY.Volume SPY.Adjusted SMA Cl.gt.SMA
但是这会创建多个连接,每次刷新pageView时我都会在服务器端获得新连接+以前的连接。
访问pageView / id时如何加入会议室?这是我的设置...
app.js
require(quantstrat)
require(quantmod)
require(FinancialInstrument)
symbols = "SPY"
initDate="2000-01-01"
from="2003-01-01"
to="2016-12-31"
options(width=70)
options("getSymbols.warning4.0"=FALSE)
#set account currency and system timezone
currency('USD')
stock("SPY",currency="USD",multiplier=1)
Sys.setenv(TZ="UTC")
#trade sizing and initial equity settings
tradeSize <- 1e6
initEq <- tradeSize*length(symbols)
#Brians code
#>> http://r.789695.n4.nabble.com/R-Quantstrat-package-question-td3772989.html
indMerge <- function(x, period, k, SMAlength, maType){
mktdata <- getSymbols(x, auto.assign = FALSE)
xW = to.period(mktdata, period = period, k = k, indexAt = "startof")
smaW = wSMA = SMA(Cl(xW), n = SMAlength, maType = maType)
x <- cbind(mktdata, smaW[paste(first(index(mktdata)) ,
last(index(mktdata)) , sep='/')])
colnames(x)[ncol(x)] = paste("SPY", ".", "SMA", sep = "")
x <- na.locf(x)
x
}
#get the data
getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE)
#apply the weekly SMA to the data without the use of add.indicator
SPY = indMerge(x = symbols, period = "weeks", k = 1, SMAlength = 14, maType = "SMA")
#set up the portfolio, account and strategy
strategy.st <- portfolio.st <- account.st <- "mtf.strat"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)
#add signals
add.signal(strategy.st, name = "sigComparison", arguments = list(columns =
c("SPY.Close", "SPY.SMA"), relationship = "gt"), label = "Cl.gt.SMA")
add.signal(strategy.st, name = "sigComparison", arguments = list(columns =
c("SPY.Close", "SPY.SMA"), relationship = "lt"), label = "Cl.lt.SMA")
#test = applySignals(strategy.st, mktdata = SPY)
#add.rules
add.rule(strategy.st, name = "ruleSignal", arguments = list(sigCol = "Cl.gt.SMA",
sigval = 1, orderqty = 900, ordertype = "market", orderside = "long"),
type = "enter")
add.rule(strategy.st, name = "ruleSignal", arguments = list(sigCol = "Cl.lt.SMA",
sigval = 1, orderqty = "all", ordertype = "market", orderside = "long"),
type = "exit")
strat = getStrategy(strategy.st)
summary(strat)
#apply the strategy and get the transactions
applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
getTxns(Portfolio = portfolio.st, Symbol = "SPY")
/ bin中/万维网
router.get('/:id', function (req, res, next) {
res.io.on('connection', function(socket) {
console.log("user connected");
//join room here
});
});
pageView.hbs
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.use(function(req, res, next){
res.io = io;
next();
});
module.exports = {app: app, server: server};
pageView.js
var app = require('../app').app;
var http = require('http');
var server = require('../app').server;
server.listen(port);
答案 0 :(得分:0)
好的,我现在明白了,你的错误就是在路线中创建听众。在io上只需要一组事件监听器。所以,这可行:
<?session_start();
$username=$_SESSION[username];
if($username == "usern1"){?>
<ul class="nav">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<? }elseif($username =="usern2"){?>
show just a half of this .nav
<? }?>
请记住,您可以在连接上传递参数,例如,您可以执行以下操作:
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(socket) {
console.log("user connected");
socket.join(room);
});
module.exports = {app: app, server: server};
并在服务器上:
var myroom = window.location.pathname.split('pageView/')[1]; //example to get room name, be creative !
var socket = io.connect("http://127.0.0.1:3000/", { query: 'room='+myroom+' });
pageView.js
io.on('connection', function(socket) {
console.log("user connected");
var room = socket.handshake.query.room; // === 'myRoom'
socket.join(room);
socket.emit("mySocket", {
device: device
});
});