我运行的是使用websocket连接的程序。以前,我的程序运行正常,但今天我收到错误消息websocket连接关闭不洁,需要升级426.在客户端我使用高速公路扭曲版本15.4.0和服务器我在节点js使用ws websocket。 请给我一些建议。 三江源。
这是我的客户代码:
##WebSocket Class
class MyClientProtocol (WebSocketClientProtocol):
def onConnect(self, response):
status = "Server connected: {0}".format(response.peer)
logData(status)
def onOpen(self):
status ="WebSocket connection open."
logData (status)
def SentData ():
select()
if (numberOfData==0): #jika data kosong koneksi terputus
# if there is no data, connection will closed
self.sendClose()
else:
self.sendMessage (dataSent.encode('utf8')) #send data if file not empty
SentData ()
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
# if server sent ack connection will close
if (payload.decode('utf8')=="ok"):
print("Text message received")
status="data sent"
logData(status)
dataNew[:]=[]
update()
# get time while sent
#p=subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
#output= str(p.communicate())
#logData(output)
self.sendClose ()
else:
# if server sent nack, data will resend
self.sendMessage(dataSent.encode('utf8'))
def onClose(self, wasClean, code, reason):
status ="WebSocket connection closed code [{}]: {}".format(code,reason)
logData (status)
self.factory.reactor.callLater (int(interval),webSocketConnect)#send data every 10 seconds
#websocket connection function
def webSocketConnect ():
factory = WebSocketClientFactory (u"wss://node-imamabdul-2.c9.io:8080", debug=False)
factory.protocol = MyClientProtocol
reactor.connectTCP("node-imamabdul-2.c9.io",8080, factory)
#SIGINT
def SIGINT_CustomEventHandler(num, frame):
k={1:"SIGHUP", 2:"SIGINT"}
status="Recieved signal - " + k[num]
logData(status)
if frame is not None:
status="SIGINT at %s:%s"%(frame.f_code.co_name, frame.f_lineno)
logData(status)
status="In SIGINT Custom Handler Shutting Down ..."
logData (status)
if num == 2:
#status= "shutting down ...."
exitFlag=True
reactor.stop()
## main
if __name__ == '__main__':
readID()
conn = sqlite3.connect(vcspath+'vcsdb2.db')
cur= conn.cursor()
create ()
## global variables
#os.system("hwclock -w -f /dev/rtc1")
exitFlag = False
Data =[]
DataStatus=False
#makeDir ()
webSocketConnect()
signal.signal(signal.SIGINT, SIGINT_CustomEventHandler)
reactor.run()
这是我的服务器代码:
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: process.env.PORT });
port = process.env.PORT;
console.log("PORT :" + port);
wss.on('connection', function connection(wss) {
console.log("connection opened" + wss.listeners());
wss.on('message', function incoming(message) {
console.log("connection has message: " + message)
var fs = require('fs');
var timestamp = new Date().toString('hex')
fs.appendFile('fromclient.csv',"['"+timestamp+"']"+' ' + message+'\n')
wss.send(message);
});
wss.on('close', function closeSocket() {
console.log("connection closed");
});
wss.on('error', function socketError() {
console.log("connection has error");
});
});
答案 0 :(得分:0)
在您的代码中,您使用的是加密的websockets连接(在您的python代码中):
#websocket connection function
def webSocketConnect ():
factory = WebSocketClientFactory (u"wss://node-imamabdul-2.c9.io:8080", debug=False)
factory.protocol = MyClientProtocol
reactor.connectTCP("node-imamabdul-2.c9.io",8080, factory)
由于您使用的是wss
的Autobahn 15.4.0,因此您遇到了the issue (bug) that exists in Autobahn version 15.4.0。
问题已在9天前修复。
您需要将修复后的版本更新为Autobahn(真正的新版本,如果已发布),或者在问题出现之前将Autobahn降级为版本(该错误)。
另一种选择是使用ws
而不是wss
- 这意味着您的连接不会被加密,这不是推荐的。