开发者

How do I configure cometd to work properly with google chrome?

开发者 https://www.devze.com 2023-01-30 14:13 出处:网络
Note:I\'ve cross-posted this question in the grails-user mailing list This weekend using this fantastic blog post as a guide and the cometd grails plug-in, I was able to ge开发者_开发技巧t cometd ins

Note: I've cross-posted this question in the grails-user mailing list

This weekend using this fantastic blog post as a guide and the cometd grails plug-in, I was able to ge开发者_开发技巧t cometd installed with my existing grails app. Everything works perfectly in FireFox and IE (believe it or not) but I'm having issues with Google Chrome. From what I can tell it's happening on the comet.init() line in my javascript.

Using Chrome's developer tools, I'm seeing this error: Wrong url scheme for WebSocket http://localhost:8080/myapp/cometd/handshake

I've done alot of searching, and found a post stating that Chrome has issues with WebSocket and the localhost. With that knowledge, I edited my hosts file to look something like:

127.0.0.1 local.mydomain.com

The url for comet.init() then becomes http://local.mydomain.com:8080/myapp/cometd/handshake. However, this did not correct the issue, and Chrome was having the same issue, but this time it looked like it didn't like the port being specified. I changed my local app to run on port 80, and the url then became http://local.mydomain.com/myapp/cometd/handshake, but still no dice.

From what I can tell, the cometd 2.0 emulates WebSocket by using long polling in browsers that don't support WebSocket natively (Firefox, IE, etc), and uses WebSocket in the browsers that do support it (Chrome), so I'm guessing that's where my issue is coming from.

Searching has also lead me to believe that WebSocket urls should start with ws:// instead of http://, so maybe that's my issue, but I do not know how to configure this with tomcat/grails.

Does anyone know how I can correct my issue?


I think I've found a temporary work around using this line of code in my javascript: cometd.unregisterTransport('websocket'); But I'd still love to know how I could use the Websocket protocol with my grails project in the future


If you are using Grails and the Cometd plugin make sure you just enable websockets from the Javascript. You shold also replace the Tomcat plugin with the Jetty 7 plugin. Keep in mind if you are using long polling your configure url will start with http:// or https:// if you are using websockets it must start with wss://.

try this

var url = "";
if (Websocket === undefined) {
   url = "http://mydomain/myconext/cometd";
} else {
  url = "ws://mydomain/myconext/cometd";
} 

cometd.configure({
    url: url
});

I wold recommended using a flash bridge. It would be a lot faster than using the long polling. See the link below. https://github.com/gimite/web-socket-js

0

精彩评论

暂无评论...
验证码 换一张
取 消