How to Call Web Service in Web Socket in iOS? - ios

currently i am making chat application using Web Socket.
My question is How to Call Web Service in Web Socket ?

In iOS using swift you can use two Cocoapods Library which makes your work hassle free and
1) Starscream
2) RocketSocket
With reference to Starscream follow are very handy example:
import UIKit
import Starscream
class ViewController: UIViewController, WebSocketDelegate {
var socket: WebSocket!
override func viewDidLoad() {
super.viewDidLoad()
var request = URLRequest(url: URL(string: "http://localhost:8080")!)
request.timeoutInterval = 5
socket = WebSocket(request: request)
socket.delegate = self
socket.connect()
}
// MARK: Websocket Delegate Methods.
func websocketDidConnect(socket: WebSocketClient) {
print("websocket is connected")
}
func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
if let e = error as? WSError {
print("websocket is disconnected: \(e.message)")
} else if let e = error {
print("websocket is disconnected: \(e.localizedDescription)")
} else {
print("websocket disconnected")
}
}
func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
print("Received text: \(text)")
}
func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
print("Received data: \(data.count)")
}
// MARK: Write Text Action
#IBAction func writeText(_ sender: UIBarButtonItem) {
socket.write(string: "hello there!")
}
// MARK: Disconnect Action
#IBAction func disconnect(_ sender: UIBarButtonItem) {
if socket.isConnected {
sender.title = "Connect"
socket.disconnect()
} else {
sender.title = "Disconnect"
socket.connect()
}
}
}

Related

Websocket connection issue in iOS?

I am using web socket connection in my app. When I am trying to establish the web socket connection and it's not connected. I am using the Starscream for making the web socket connection. I've tried with many test WS Url for testing and none of the url is working. Currently I am testing in simulator. Are there any proxy issues or firewall issue?.
class ViewController: UIViewController, WebSocketDelegate {
var socket: WebSocket!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "wss://echo.websocket.org" // testing url
var request = URLRequest(url: URL(string: urlString)!)
request.timeoutInterval = 30
socket = WebSocket(request: request)
socket.delegate = self
socket.pongDelegate = self as? WebSocketPongDelegate
socket.connect()
}
// MARK: Websocket Delegate Methods.
// Never call this method
func websocketDidConnect(socket: WebSocketClient) {
print("websocket is connected")
}
func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
if let e = error as? WSError {
print("websocket is disconnected: \(e.message)")
} else if let e = error {
print("websocket is disconnected: \(e.localizedDescription)")
} else {
print("websocket disconnected")
}
}
func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
print("Received text: \(text)")
}
func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
print("Received data: \(data.count)")
}
}
Couple of things to check in case you haven't done these:
Remember apple blocks all non https urls by default. Change arbitrary loads to YES in -
info.plist -> App Transport Security Settings > Allow Arbitrary Loads
Set self.socket.selfSignedSSL = true

Delegated functions not working outside of UIViewController

I'm using the Startscream Websocket framework. Everything works fine as long as I keep all of the code in a UIViewController as seen here. But as soon as a create a wrapper class for Startscream all of the delegated functions stop working. Also my local websocket server is not getting a connection.
How can I get the code working inside a wrapper class?
MyService.swift:
import Starscream
public class MyService: WebSocketDelegate {
var socket = WebSocket(url: URL(string: "ws://localhost:3900/websocket")!)
func connect() {
socket.delegate = self
socket.connect()
print("Connecting")
}
// MARK: Websocket Delegate Methods.
public func websocketDidConnect(socket: WebSocket) {
print("websocket is connected")
}
public func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
if let e = error {
print("websocket is disconnected: \(e.localizedDescription)")
} else {
print("websocket disconnected")
}
}
public func websocketDidReceiveMessage(socket: WebSocket, text: String) {
print("Received text: \(text)")
}
public func websocketDidReceiveData(socket: WebSocket, data: Data) {
print("Received data: \(data.count)")
}
// MARK: Write Text Action
#IBAction func writeText(_ sender: UIBarButtonItem) {
socket.write(string: "hello there!")
}
// MARK: Disconnect Action
#IBAction func disconnect(_ sender: UIBarButtonItem) {
if socket.isConnected {
sender.title = "Connect"
socket.disconnect()
} else {
sender.title = "Disconnect"
socket.connect()
}
}
}
ViewController.swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let service = MyService()
service.connect()
}
}
The reference to Service in ViewController was not stored anywhere so as soon as the function was run it was cleaned up. This is how I fixed it:
class ViewController: UIViewController {
var service = MyService()
override func viewDidLoad() {
super.viewDidLoad()
service.connect()
}
...
I'm not sure what is wrong with your code, it can be an endpoint issue, or WS is not allocated somehow. I have same code with Starscream working in Swift 3, here is the main part of my class.
class ConnectionManager {
private var savedSocket: WebSocket?
fileprivate var socket: WebSocket {
if let saved = savedSocket {
return saved
}
let wsURL = URL(string: UserDefaultsManager.wsURLString)!
savedSocket = WebSocket(url: wsURL)
return savedSocket!
}
func startSession() {
if (socket.isConnected) { return }
socket.headers = headers
socket.delegate = self
socket.connect()
}
func endSession() {
if (socket.isConnected) {
socket.disconnect()
}
}
}
extension ConnectionManager: WebSocketDelegate {
func websocketDidConnect(socket: WebSocket) {
}
func websocketDidDisconnect(socket: WebSocket, error: NSError?){
if let e = error {
log.error("websocket is disconnected with ERROR: \(e.localizedDescription)")
} else {
log.error("websocket disconnected")
}
}
func websocketDidReceiveMessage(socket: WebSocket, text: String){
}
func websocketDidReceiveData(socket: WebSocket, data: Data){
}
}
My code is not a best practise for sure, but it works without any problem. Try to use my version, maybe it will work for you.

Socket won't connect with Starscream

I am using Starscream to open up a websocket. The problem is that it does't connect on localhost or with the server.
var socket = WebSocket(url: NSURL(string: "ws://localhost:8080/")!)
self.socket.voipEnabled = true;
socket.delegate = self
socket.connect()
func websocketDidConnect(ws: WebSocket) {
print("websocket is connected")
}
func websocketDidDisconnect(ws: WebSocket, error: NSError?) {
print("websocket is disconnected: \(error?.localizedDescription)")
}
func websocketDidReceiveMessage(ws: WebSocket, text: String) {
print("Received text: \(text)")
}
func websocketDidReceiveData(ws: WebSocket, data: NSData) {
print("Received data: \(data.length)")
}
func websocketDidReceivePong(socket: WebSocket) {
print("Got pong!")
}
#IBAction func writeText(sender: UIBarButtonItem) {
socket.writeString("hello there!")
}
it started connected once I added self.socket.selfSignedSSL = true;

Starscream delegates not being called

The variable is not nil, I have a good connection and the url is correct but no delegate methods are being called. Also I am implementing WebSocketDelegate
let socket = WebSocket(url: NSURL(string: "UrlHere:port/")!)
socket.delegate = self;
socket.connect()
if socket.isConnected {
print("websocket is connected")
}
func websocketDidConnect(ws: WebSocket) {
print("websocket is connected")
}
func websocketDidDisconnect(ws: WebSocket, error: NSError?) {
if let e = error {
print("websocket is disconnected: \(e.localizedDescription)")
} else {
print("websocket disconnected")
}
}
func websocketDidReceiveMessage(ws: WebSocket, text: String) {
print("Received text: \(text)")
}
func websocketDidReceiveData(ws: WebSocket, data: NSData) {
print("Received data: \(data.length)")
}
func websocketDidReceivePong(socket: WebSocket) {
print("Got pong!")
}
Socket should be a property or variable of your class to make sure it sticks around.
If you allocate it just on a function stack it will fall out of scope and the delegates will never get called
Here is the code that I have used in my project just in case
import UIKit
//import WebSocket
import Starscream
class ViewController: UIViewController,WebSocketDelegate,WebSocketPongDelegate {
#IBOutlet weak var wsURL: UITextField!
#IBOutlet weak var wsConsole: UITextView!
#IBOutlet weak var wsMessage: UITextField!
var socket:WebSocket!
override func viewDidLoad() {
super.viewDidLoad()
// var webSocketObj:WebSocket = WebSocket()
// webSocketObj.ipAddressText = "10.12.1.101"
// webSocketObj.portText = "8888"
// webSocketObj.dataToSendText = "hi"
// webSocketObj.dataRecievedTextView = ""
// webSocketObj.connectedLabel = ""
// webSocketObj.connectToServer()
wsURL.text="ws://10.12.1.101:8888/"
wsMessage.text="Hi"
self.navigationItem.leftBarButtonItem?.title="Connect"
self.navigationItem.rightBarButtonItem?.enabled=false
// Do any additional setup after loading the view, typically from a nib.
}
func websocketDidConnect(socket: WebSocket){
wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket got connected")
self.navigationItem.leftBarButtonItem?.title="Disconnect"
self.navigationItem.rightBarButtonItem?.enabled=true
}
func websocketDidDisconnect(socket: WebSocket, error: NSError?){
wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket got disconnected")
self.navigationItem.leftBarButtonItem?.title="Connect"
self.navigationItem.rightBarButtonItem?.enabled=false
}
func websocketDidReceiveMessage(socket: WebSocket, text: String){
wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket got a message from server:").stringByAppendingString(text)
}
func websocketDidReceiveData(socket: WebSocket, data: NSData){
print("websocket received data",data)
}
#IBAction func writeText(sender: UIBarButtonItem) {
wsConsole.text = wsConsole.text .stringByAppendingString("\n Client sent a message:").stringByAppendingString(wsMessage.text!)
socket.writeString(wsMessage.text!)
self.view .endEditing(true)
}
#IBAction func disconnect(sender: UIBarButtonItem) {
self.view .endEditing(true)
if socket == nil{
connect(sender)
}
else if socket.isConnected {
socket.disconnect()
} else {
connect(sender)
}
}
func connect(sender:UIBarButtonItem){
socket = WebSocket(url: NSURL(string:wsURL.text!)!)
socket.delegate = self
socket.connect()
}
func websocketDidReceivePong(socket: WebSocket){
wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket received pong")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the link to storyboard just in case if you want

Starscream's socket.isConnected is always returning false

I'm developing an iOS application using Swift that connects to an Android device via sockets. I've implemented Starscream and was able to connect to the other device. My issue is that when I print something on the websocketDidConnect delegate method, it does not print. Also when I print socket.isConnected, it prints false. Kindly help me. Thanks. Other alternatives to Starscream are also welcome, thanks!
Here is my code:
import Starscream
class SettingsViewController: UIViewController, UITextFieldDelegate, WebSocketDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let socket = WebSocket(url: NSURL(string: "ws://\(ipAddressTextField.text!):4000/")!)
socket.connect()
print("socket.isConnected \(socket.isConnected)") //this one is false
socket.onConnect = {
print("Connected...") // does not print
}
socket.onDisconnect = { (error: NSError?) in
print("websocket is disconnected: \(error?.localizedDescription)") // this one prints when I turn the wifi off
}
socket.onData = { (data: NSData) in
print("got some data: \(data.length)")
}
}
func websocketDidConnect(ws: WebSocket) {
print("websocket is connected")
}
func websocketDidDisconnect(ws: WebSocket, error: NSError?) {
if let e = error {
print("websocket is disconnected: \(e.localizedDescription)")
} else {
print("websocket disconnected")
}
}
func websocketDidReceiveMessage(ws: WebSocket, text: String) {
print("Received text: \(text)")
}
func websocketDidReceiveData(ws: WebSocket, data: NSData) {
print("Received data: \(data.length)")
}
}
You need to make it as a class property otherwise it will be released as it gets out of viewDidLoad method.

Resources