Here is my source code, when I use the browser to initiate a query request, if a small amount of data returned from the service program, the browser receives the data will not be lost, and is returned by the asynchronous, very normal. But when a large amount of data the response data returned from the service program, is also an asynchronous returned, the browser receives the data will have lost, which makes browser JSON parsing incorrect. Excuse me, is this what caused? Nim or JESTER or my code? code:
# 实时库存查询服务器
import jester, asyncdispatch, asyncnet
import db_odbc, threadpool, cgi, encodings, strutils
import logging
var L = newConsoleLogger()
var rL = newRollingFileLogger("log/cx.log", fmtStr = verboseFmtStr)
addHandler(L)
addHandler(rL)
proc gl(s:string):string =
if s == "" or s == nil:
result = ""
else:
var str:string
str = replace(s,"\"","”")
str = replace(str,"\'","’")
str = replace(str,"\13\10","")
str = replace(str,"\13","")
str = replace(str,"\10","")
result = str
proc cx(countsql,mysql:string):string {.thread.} =
var theDb = open("dbpos", "dbcxj", "dbcxj", "dbpos")
var rowcount,sm,cbsj,bb,sl:string
if countsql != "":
rowcount = theDb.getValue(sql(countsql)) #获取行数
else:
rowcount = "-1"
var jsonstr = "{\"rowcount\":\"" & rowcount & "\",\"data\":["
var isFirstRow:bool = true
for row in theDb.fastRows(sql(mysql)):
if isFirstRow:
jsonstr.add "{"
isFirstRow = false
else:
jsonstr.add ",{"
# 替换可能引起错误的字符
sm = gl($row[2])
cbsj = gl($row[4])
bb = gl($row[5])
sl = gl($row[6])
if sl == "":
sl = "0"
jsonstr.add "\"goodsid\":\"" & $row[0] & "\","
jsonstr.add "\"barcode\":\"" & $row[1] & "\","
jsonstr.add "\"goodslname\":\"" & sm & "\","
jsonstr.add "\"adviceprice\":\"" & $row[3] & "\","
jsonstr.add "\"cbsj\":\"" & cbsj & "\","
jsonstr.add "\"bb\":\"" & bb & "\","
jsonstr.add "\"qty\":\"" & sl & "\","
jsonstr.add "\"shopid\":\"" & $row[7] & "\"}"
jsonstr.add "]}"
theDb.close()
result = jsonstr
proc isComplete(fv:FlowVar[system.string]):Future[string]{.async.}=
await sleepAsync(10)
while true: #不停的查询线程执行的状态,直到执行完成,返回查询结果
if isReady(fv):
result = ^fv
break
await sleepAsync(10)
settings:
port = Port(8080)
appName = "/bookcx"
routes:
get "/cx/?@isbn?/?@sm?/?@pg1?/?@pg2?/?":
var isbn,sm,countsql,countsql_temp,sql,pg1, pg2:string
countsql_temp = "SELECT count(*) as recnum FROM ("&
"select goods.goodsid,shopstock.shopid from goods left join shopstock on goods.goodsid=shopstock.goodsid "&
"where shopstock.qty is not null"
sql = "SELECT * FROM ( SELECT A.*, ROWNUM RN FROM ("&
"select goods.goodsid,goods.barcode,goods.goodslname,goods.adviceprice,goods.cbsj,goods.bb,"&
"shopstock.qty,shopstock.shopid from goods left join shopstock on goods.goodsid=shopstock.goodsid "&
"where shopstock.qty is not null"
isbn = @"isbn"
sm = cgi.decodeUrl(@"sm").convert(encodings.getCurrentEncoding(),"UTF-8") # 将url编码解码后变为utf-8,再将utf-8转码为当前系统编码gb2312
if (@"pg1" == nil or @"pg1" == ""):
pg1 = "1"
else:
pg1 = @"pg1"
if (@"pg2" == nil or @"pg2" == ""):
pg2 = "10"
else:
pg2 = @"pg2"
if isbn != nil and isbn != "":
countsql_temp.add " and goods.barcode='" & isbn & "'"
sql.add " and goods.barcode='" & isbn & "'"
if sm != nil and sm != "":
countsql_temp.add " and goods.goodslname like '%" & sm & "%')"
sql.add " and goods.goodslname like '%" & sm & "%' ) A)"
#sql.add ") A WHERE ROWNUM <= " & pg2 & " ) WHERE RN >= " & pg1 & ""
if (isbn == nil or isbn == "") and (sm == nil or sm == ""):
resp "{\"rowcount\":\"0\",\"data\":[]}","text/html;charset=gb2312"
else:
countsql=""
if pg1 == "1": # 第一次查询将查询总行数
countsql = countsql_temp
logging.info(request.ip&" Keyword --> isbn:"&isbn&", sm:"&sm&", pg1:"&pg1&", pg2:"&pg2)
var fv = spawn cx(countsql,sql) #开新线程用来查询
var dat = await isComplete(fv) #等待查询线程结束并返回查询结果
resp $dat,"text/html;charset=gb2312" #向客户端发送结果
runForever()