-->

How to use SocketRocket with socket.io?

2019-02-03 15:42发布

问题:

The new iOS websocket library, SocketRocket, looks really awesome, and the chat example project is really sweet. The example chat server is written in Python, of which I know none. I'm slightly familiar with socket.io on node.js and prefer to try javascript. I tried connecting the SocketRocket TestChat simulator app to the node.js(6.10) socket.io(0.8.7) but the connection was refused. (Also, I'm not a node expert either, so this may be a silly question).

In a recent answer to an SO question, mikelikespie said to op: "I suggest updating your stack to use the iOS WebSocket library we just released."

What does that mean exactly? What is necessary to get the SocketRocket library to talk to a node socket.io server? Any sample code or suggestions are greatly appreciated.

回答1:

Well you socket.io has it's own protocol built on transport protocols such as websockets, or long polling, well SocketRocket is just a websocket library, no more. Socket.io also has some authentication, so it's not possible to use it, with normal web sockets. What I suggest you to use is a just websocket server, no more than that. Such as ws, which should compatible. There are other libraries, but, ws I believe is actively developed at the moment, and newer than others.

You can also make your clientside socket.io compatible, but I think its uneeded. Here's the specs for it: https://github.com/LearnBoost/socket.io-spec

Hope I helped.



回答2:

socket.IO-objc on GitHub looks to be a mature Socket.IO / Objective C Library

It uses SocketRocket to handle the websocket connections.

This will allow you to have a socket server that supports older browsers (degrades to long-polling etc.) and is still accessable to your iOS app via SocketRocket.



回答3:

I have just implemented an active socket connection between a Node.JS server, and an iOS application, using this library. I used this one because the README on Socket.IO-ObjC says

The current version of this library does not yet support socket.io v1.0. So if you want to use socket.io-objc, please fall back to v0.9.x.

but I always want to use the latest version for everything. So if you want to use Socket.IO v1.0, you can use the one on the link provided few line above.