先设置 Crossbar 服务的 config.json 文件,transports paths 属性里面添加 call 配置,设置 type realm role
"call": {
"type": "caller",
"realm": "realm1",
"role": "public"
}
config.json
{
"$schema": "https://raw.githubusercontent.com/crossbario/crossbar/master/crossbar.json",
"version": 2,
"controller": {},
"workers": [
{
"type": "router",
"realms": [
{
"name": "realm1",
"roles": [
{
"name": "authenticator",
"permissions": [
{
"uri": "com.xxxx.authenticate",
"match": "exact",
"allow": {
"call": false,
"register": true,
"publish": false,
"subscribe": false
},
"disclose": {
"caller": false,
"publisher": false
},
"cache": true
}
]
},
{
"name": "public",
"permissions": [
{
"uri": "com....anonymous.",
"match": "wildcard",
"allow": {
"call": true,
"register": false,
"publish": false,
"subscribe": false
},
"disclose": {
"caller": false,
"publisher": false
},
"cache": true
}
]
}
]
}
],
"transports": [
{
"type": "web",
"endpoint": {
"type": "tcp",
"port": 8080
},
"paths": {
"/": {
"type": "static",
"directory": "../web"
},
"ws": {
"type": "websocket",
"serializers": [
"json"
],
"auth": {
"ticket": {
"type": "static",
"principals": {
"backend": {
"ticket": "xxxx.com",
"role": "backend"
}
}
},
"anonymous": {
"type": "static",
"role": "public"
},
"wampcra": {
"type": "dynamic",
"authenticator": "com.xxxx.authenticate"
}
}
},
"call": {
"type": "caller",
"realm": "realm1",
"role": "public"
}
}
}
]
}
]
}
使用 node.js 注册一个匿名接口 add_d
function add (args) {
console.log('args: ', args)
return args[0] + args[1]
}
session.register('com.xxxx.xxxx.xxxx.anonymous.add_d', add)
session.call('com.xxxx.xxxx.xxxx.anonymous.add_d', [3, 4])
.then(
function (res) {
console.log('res: ', res) // res: 7
},
function (err) {
console.log('err: ', err)
})
前端 home.vue 中使用 axios post 调用对应接口接口即可。crossbar 接口名称写在 procedure 属性中,传递的接口参数写在 args 属性中
http://xxxx.xxxx.com/ crossbar 服务域名
this.axios.post('http://xxxx.xxxx.com/call', {
procedure: 'com.xxxx.xxxx.xxxx.anonymous.add_d',
args: [1, 2]
}).then((response) => {
console.log(response.data[0]) // 3
}).catch((err) => {
console.log(err)
})
注意
服务端如果为默认设置,会出现跨域错误
about:1 Access to XMLHttpRequest at 'http://xxxx.xxxx.com/call' from origin 'http://192.168.199.172:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
About.vue?c330:46 err
About.vue?c330:47 Error: Network Error
at createError (createError.js?2d83:16)
at XMLHttpRequest.handleError (xhr.js?b50d:87)
服务端 nginx 跨域请求配置如下,设置好后,前端就可以使用 http post 请求连接后端提供的 crossbar 接口了
server {
listen 80;
server_name xxxx.xxxx.com;
allow 10.10.0.0/16;
deny all;
location / {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "GET,POST,PUT,DELETE,PATCH,OPTIONS";
add_header Access-Control-Allow-Headers 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type';
return 200;
}
proxy_set_header Access-Control-Allow-Origin *;
proxy_pass http://10.10.10.10:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 4s;
proxy_read_timeout 86400s;
proxy_send_timeout 12s;
}
}