WSO2 websocket + SockJS - 跨域请求被阻止

3
自从WSO2 5.0支持WebSockets,我按照教程编写了一个简单的应用程序: 下面是来自WSO2的源代码输出:
<?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>

我已经成功地使用Nett客户端进行了测试:

C:\work\servers\netty>java -Durl=ws://localhost:9091/websocket -DsubProtocol="synapse(contentType='application/xml')" -cp netty-example-4.1.4.Final.jar;lib/*;. io.netty.example.http.websocketx.client.WebSocketClient
WebSocket Client connected!

然而,如果我试图从JavaScript代码测试它,我会收到错误提示,如下图所示:
enter image description here
你知道我的代码有什么问题吗?
1个回答

2

出现这个问题的原因是因为SockJS在尝试使用XMLHttpRequest加载URL时,但Chrome不允许访问跨域内容,除非协议是上述协议之一(在此情况下是ws://))。

我已经尝试过类似的Firefox场景,它可以正常工作,因为它没有这个Chrome特定的限制。

在这种情况下,由于WSO2 ESB公开了WebSocket接口来调用HTTP Endpoint,您可以使用如下本地HTML5 WebSocket实现。

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

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接