WSO2 websocket + SockJS - 交叉原始请求被阻止

时间:2016-08-30 08:16:54

标签: websocket wso2 wso2esb sockjs

由于WSO2 5.0支持WebSockets,我编写了一个简单的应用程序,后面是教程:

这是WSO2的源视图输出:

urls:{ 
    signup:string, 
    token:string, 
    requestResetPassword: string,
    resetPassword: string,
    changePassword: string,
    socialLoginWithCode: string,
    socialSignupWithCode: string,
    socialLoginWithToken: string
} = {
    signup: '/1/user/signup',
    token: '/token',
    requestResetPassword: '/1/user/requestResetPassword',
    resetPassword: '/1/user/resetPassword',
    changePassword: '/1/user/changePassword',
    socialLoginWithCode: '/1/user/PROVIDER/code',
    socialSignupWithCode: '/1/user/PROVIDER/signupCode',
    socialLoginWithToken: '/1/user/PROVIDER/token'
};

我能够使用Nett客户端成功测试它:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <registry provider="org.wso2.carbon.mediation.registry.WSO2Registry">
        <parameter name="cachableDuration">15000</parameter>
    </registry>
    <taskManager provider="org.wso2.carbon.mediation.ntask.NTaskTaskManager"/>
    <sequence name="fault">
        <!-- Log the message at the full log level with the ERROR_MESSAGE and the ERROR_CODE-->
        <log level="full">
            <property name="MESSAGE" value="Executing default 'fault' sequence"/>
            <property expression="get-property('ERROR_CODE')" name="ERROR_CODE"/>
            <property expression="get-property('ERROR_MESSAGE')" name="ERROR_MESSAGE"/>
        </log>
        <!-- Drops the messages by default if there is a fault -->
        <drop/>
    </sequence>
    <sequence name="main">
        <in>
            <!-- Log all messages passing through -->
            <log level="full"/>
            <!-- ensure that the default configuration only sends if it is one of samples -->
            <!-- Otherwise Synapse would be an open proxy by default (BAD!)               -->
            <filter regex="http://localhost:9000.*" source="get-property('To')">
                <!-- Send the messages where they have been sent (i.e. implicit "To" EPR) -->
                <send/>
            </filter>
        </in>
        <out>
            <send/>
        </out>
        <description>The main sequence for the message mediation</description>
    </sequence>
    <sequence name="outDispatchSeq">
        <log level="full"/>
        <respond/>
    </sequence>
    <sequence name="dispatchSeq">
        <switch
            source="get-property('websocket.source.handshake.present')" xmlns:ns="http://org.apache.synapse/xsd">
            <case regex="true">
                <drop/>
            </case>
            <default>
                <call/>
                <respond/>
            </default>
        </switch>
    </sequence>
    <!-- You can add any flat sequences, endpoints, etc.. to this synapse.xml file if you do
    *not* want to keep the artifacts in several files -->
    <inboundEndpoint name="test" onError="fault" protocol="ws"
        sequence="dispatchSeq" suspend="false">
        <parameters>
            <parameter name="inbound.ws.port">9091</parameter>
            <parameter name="ws.client.side.broadcast.level">0</parameter>
            <parameter name="ws.outflow.dispatch.sequence">outDispatchSeq</parameter>
            <parameter name="ws.outflow.dispatch.fault.sequence">fault</parameter>
        </parameters>
    </inboundEndpoint>
</definitions>

但是,如果我尝试从JavaScript代码测试它,我会收到如下错误:

enter image description here

你知道我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:2)

出现此问题是因为SockJS在内部尝试使用XMLHttpRequest加载网址,但Chrome不允许访问跨源内容,除非该协议是上述协议之一(在这种情况下,它是ws://)我在Firefox上尝试了类似的情况,但由于没有Chrome特定的限制,因此工作正常。

在这种情况下,由于WSO2 ESB公开WebSocket接口以调用HTTP端点,因此您可以按如下方式使用本机HTML5 WebSocket实现。

var url = 'ws://localhost:9091/websocket';
var ws = new WebSocket(url);

ws.onopen = function() {
  // todo
}

ws.onmessage = function(e) {
  // todo
}