使用横杆时我遇到了一些问题。我开始运行高速公路 python 后端和高速公路 js 前端,一切似乎都很正常,直到我收到此错误:
2016-08-01T14:12:40+0000 [Router 9051] failing WebSocket opening handshake ('WebSocket connection denied: origin 'null' not allowed')
知道为什么吗?以及如何解决?
编辑:
这是一些代码(我正在使用 WAMP),但我认为问题与代码无关,因为它已在其他网络中工作,只是现在不在我所在的网络中。无论如何:
前端:
<!DOCTYPE html>
<html>
<body>
<h1>Hello WAMP/Browser - Frontend</h1>
<p>Open JavaScript console to watch output.</p>
<script>AUTOBAHN_DEBUG = true;</script>
<script src="http://autobahn.s3.amazonaws.com/autobahnjs/latest/autobahn.min.jgz"></script>
<script>
// the URL of the WAMP Router (Crossbar.io)
//
var wsuri = "ws://192.168.4.24:8080/";
// the WAMP connection to the Router
//
var connection = new autobahn.Connection({
url: wsuri,
realm: "joyPadRealm"
});
// timers
//
var t1, t2;
// fired when connection is established and session attached
//
connection.onopen = function (session, details) {
console.log("Connected");
// SUBSCRIBE to a topic and receive events
//
function on_counter (args) {
var counter = args[0];
console.log("on_counter() event received with counter " + counter);
}
session.subscribe('com.example.oncounter', on_counter).then(
function (sub) {
console.log('subscribed to topic');
},
function (err) {
console.log('failed to subscribe to topic', err);
}
);
// PUBLISH an event every second
//
t1 = setInterval(function () {
session.publish('com.example.onhello', ['Hello from JavaScript (browser)']);
console.log("published to topic 'com.example.onhello'");
}, 1000);
// REGISTER a procedure for remote calling
//
function mul2 (args) {
var x = args[0];
var y = args[1];
console.log("mul2() called with " + x + " and " + y);
return x * y;
}
session.register('com.example.mul2', mul2).then(
function (reg) {
console.log('procedure registered');
},
function (err) {
console.log('failed to register procedure', err);
}
);
// CALL a remote procedure every second
//
var x = 0;
t2 = setInterval(function () {
session.call('com.example.add2', [x, 18]).then(
function (res) {
console.log("add2() result:", res);
},
function (err) {
console.log("add2() error:", err);
}
);
x += 3;
}, 1000);
};
// fired when connection was lost (or could not be established)
//
connection.onclose = function (reason, details) {
console.log("Connection lost: " + reason);
if (t1) {
clearInterval(t1);
t1 = null;
}
if (t2) {
clearInterval(t2);
t2 = null;
}
}
// now actually open the connection
//
connection.open();
</script>
</body>
</html>
后端:
from twisted.internet.defer import inlineCallbacks
from twisted.logger import Logger
from os import environ
from autobahn.twisted.util import sleep
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
from autobahn.wamp.exception import ApplicationError
class AppSession(ApplicationSession):
log = Logger()
@inlineCallbacks
def onJoin(self, details):
## SUBSCRIBE to a topic and receive events
##
def onhello(msg):
self.log.info("event for 'onhello' received: {msg}", msg=msg)
sub = yield self.subscribe(onhello, 'com.example.onhello')
self.log.info("subscribed to topic 'onhello'")
## REGISTER a procedure for remote calling
##
def add2(x, y):
self.log.info("add2() called with {x} and {y}", x=x, y=y)
return x + y
reg = yield self.register(add2, 'com.example.add2')
self.log.info("procedure add2() registered")
## PUBLISH and CALL every second .. forever
##
counter = 0
while True:
## PUBLISH an event
##
yield self.publish('com.example.oncounter', counter)
self.log.info("published to 'oncounter' with counter {counter}",
counter=counter)
counter += 1
## CALL a remote procedure
##
try:
res = yield self.call('com.example.mul2', counter, 3)
self.log.info("mul2() called with result: {result}",
result=res)
except ApplicationError as e:
## ignore errors due to the frontend not yet having
## registered the procedure we would like to call
if e.error != 'wamp.error.no_such_procedure':
raise e
yield sleep(1)
if __name__ == '__main__':
runner = ApplicationRunner(
environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://192.168.4.24:8080/"),
u"joyPadRealm",
)
runner.run(AppSession)
配置文件
{
"version": 2,
"controller": {},
"workers": [
{
"type": "router",
"realms": [
{
"name": "joyPadRealm",
"roles": [
{
"name": "anonymous",
"permissions": [
{
"uri": "",
"match": "prefix",
"allow": {
"call": true,
"register": true,
"publish": true,
"subscribe": true
},
"disclose": {
"caller": false,
"publisher": false
},
"cache": true
}
]
}
]
}
],
"transports": [
{
"type": "websocket",
"endpoint": {
"type": "tcp",
"port": 8080
},
"url": "ws://localhost:8080/"
}
]
}
]
}