我在使用websocket连接的地方运行了我的程序。以前,我的程序运行正常,但今天我收到错误消息,websocket连接不清楚关闭,需要升级426。在客户端中,我使用autobahn扭曲版本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");
});
});
在您的代码中,您使用的是加密的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
的高速公路15.4.0,因此您遇到了高速公路15.4.0版本中存在的问题(bug)。
该问题在9天前得到解决。
你需要将高速公路更新到修复后的版本(如果发布的话,那就是真正的新版本),或者将高速公路降级到问题(bug)出现之前的版本。
另一种选择是使用ws
而不是wss
——这意味着你的连接不会被加密,这不是建议的。
祝你好运!