Swift Socket Sever disconnected while moving to one view controller to other - ios

I am trying to implement swift socket in my application. I am pretty new to socket programming. I have used
https://github.com/swiftsocket/SwiftSocket
for socket connection. I had create NSObject class for Server Connection.
I want Server connected through out my whole application, But Server is disconnected while I'm moving into another ViewController.
class SocketGLobal : NSObject
{
func Connect()
{
let client = TCPClient(address: "www.apple.com", port: 80)
switch client.connect(timeout: 1) {
case .success:
switch client.send(string: "GET / HTTP/1.0\n\n" ) {
case .success:
guard let data = client.read(1024*10) else { return }
if let response = String(bytes: data, encoding: .utf8) {
print(response)
}
case .failure(let error):
print(error)
}
case .failure(let error):
print(error)
}
}
}

I faced the same problem. So I created the custom ViewController and used GCDAsyncUdpSocket for socket connection. In your pod file add pod 'CocoaAsyncSocket' to use GCDAsyncUdpSocket. Here is the code snippet for custom ViewController.
import CocoaAsyncSocket
class CustomViewController: UIViewController, GCDAsyncUdpSocketDelegate {
//MARK: VARIABLE DECLARATION **********
var _udpSocket: GCDAsyncUdpSocket?
var udpSocket: GCDAsyncUdpSocket? {
get {
if _udpSocket == nil {
let port = 80
let sock = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
do {
sock.setIPv6Enabled(false)
// sock?.setIPv4Enabled(true)
// sock?.setPreferIPv4()
try sock.enableBroadcast(true)
try sock.bind(toPort: UInt16(port))
try sock.beginReceiving()
} catch let err as NSError {
print(">>> Error while initializing socket: \(err.localizedDescription)")
sock.close()
return nil
}
_udpSocket = sock
}
return _udpSocket
}
set {
_udpSocket?.close()
_udpSocket = newValue
}
}
//MARK: UDP SERVICES *********
//Start UDP Receiver
func startUDPService(){
guard udpSocket != nil else {
return
}
}
//Stop UDP Receiver
func closeUDPService(){
if udpSocket != nil {
udpSocket?.close()
udpSocket = nil
}
}
//MARK: HANDLING OF RECEIVED DATA **********
func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
var hostPtr : NSString? = nil
GCDAsyncUdpSocket.getHost(&hostPtr, port: &send_port, fromAddress: address)
var receivedText = ""
if let stringData = String(data: data as Data, encoding: String.Encoding.utf8) {
receivedText = stringData
}
print("Data received: \(String(describing: hostPtr)), \(receivedText)")
}
}
In your each ViewController implement as follows:
import UIKit
class YourViewController: CustomViewController {
//MARK: LIFE CYCLE METHODS **********
override func viewWillAppear(_ animated: Bool) {
//MARK: START UDP SERVICE
self.startUDPService()
}
override func viewWillDisappear(_ animated: Bool) {
//MARK: STOP UDP SERVICE
self.closeUDPService()
}
}
I hope this will help you.

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.

Use a protocol between a class and a view controller Swift 3

I worked with the library SwiftSocket to send messages to my server. When the connexion is established I want to create a callback to catch results. But my protocol is always null and the callback is not send.
This is my TCPClient class :
protocol ProtocolTCPClient {
func connexionSucceeded()
}
class UtilsTCPClient {
var delegate: ProtocolTCPClient?
let client: TCPClient?
init(address: String, port: Int32) {
client = TCPClient(address: address, port: port)
switch client!.connect(timeout: 5) {
case .success:
print("Success")
if(delegate != nil){
self.delegate?.connexionSucceeded()//never called
} else{
print("delegate nil")
}
case .failure(let error):
print("Error: ")
print(error)
}
}
}
This is my ViewController
import UIKit
import SwiftSocket
class ViewController: UIViewController, ProtocolTCPClient {
var client: UtilsTCPClient?
override func viewDidLoad() {
super.viewDidLoad()
self.client = UtilsTCPClient(address: "server", port: 80)
self.client?.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func connexionSucceeded(){
print("Connexion succeeded")//never called
}
}
Thank you for your answers
your code should look like this.Because in your code first you init 'UtilsTCPClient' and then assigning delegate. And in 'UtilsTCPClient' you create 'TCPClient' and trying to access delegate but till it't not assigned so you should try the below code.
protocol ProtocolTCPClient {
func connexionSucceeded()
}
class UtilsTCPClient {
var delegate: ProtocolTCPClient?
let client: TCPClient?
init(address: String, port: Int32, delegate: ProtocolTCPClient) {
self.delegate = delegate
client = TCPClient(address: address, port: port)
switch client!.connect(timeout: 5) {
case .success:
print("Success")
if(delegate != nil){
self.delegate?.connexionSucceeded()
} else{
print("delegate nil")
}
case .failure(let error):
print("Error: ")
print(error)
}
}
}
class ViewController: UIViewController, ProtocolTCPClient {
var client: UtilsTCPClient?
override func viewDidLoad() {
super.viewDidLoad()
self.client = UtilsTCPClient(address: "server", port: 80, delegate: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func connexionSucceeded(){
print("Connexion succeeded")//never called
}
}

How to receive data from server through socket in iOS?

I have an application in which i have to send data to server using socket connection and read data which will send by server. I am using AsyncSocket class for write and read data. I got success to write data on server and server can see the data which was send by application. Now, problem is that whenever server send some data to application then how can i receive those data using AsyncSocket class. Below is my code. I put delegates method for reading data but it never called.
var socket = AsyncSocket()
socket = AsyncSocket(delegate: self)
self.socketConnect()
func socketConnect() {
do {
try socket?.connectToHost("IP Address", onPort: 6968)
} catch _ as NSError {
}
}
//MARK: - AsyncSocket Delegates method
func onSocket(sock: AsyncSocket!, didConnectToHost host: String!, port: UInt16) {
print("Connected to host : \(host) with Port : \(port)")
let alert:UIAlertController = UIAlertController(title: "Connected", message: "Host:\(host) ** Port:\(port)", preferredStyle:.Alert)
self.presentViewController(alert, animated: true, completion: nil)
let action:UIAlertAction = UIAlertAction(title: "Ok", style: .Default) { (UIAlertAction) -> Void in
print("Ok Pressed")
}
alert .addAction(action)
let dict = ["iUserId":"100","iRideId":"276","type":"client"] // For client side
var jsonString = NSString()
do {
let data = try NSJSONSerialization.dataWithJSONObject(dict , options: NSJSONWritingOptions.PrettyPrinted)
jsonString = NSString(data: data, encoding: NSUTF8StringEncoding)!
}catch let error as NSError {
print(error)
}
let reqData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)
socket.writeData(reqData, withTimeout: 1.0, tag: 100)
}
func onSocket(sock: AsyncSocket!, didReadPartialDataOfLength partialLength: UInt, tag: Int) {
print("Read partial data")
}
func onSocket(sock: AsyncSocket!, didWriteDataWithTag tag: Int) {
print("Data write successfully")
}
func onSocket(sock: AsyncSocket!, didReadData data: NSData!, withTag tag: Int) {
print("Data read successfully")
}
func onSocket(sock: AsyncSocket!, willDisconnectWithError err: NSError!) {
print("Socket disconnect with error :\(err.description)")
}
func onSocket(sock: AsyncSocket!, didAcceptNewSocket newSocket: AsyncSocket!) {
print("Accept new socket")
}
I have found answer for my self. What i have missed is that i have to put readdata line when socket is connected to host. So after write this didReadData method will be called and in that method also have to write a single line code that i wrote in didConnectToHost method.
func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) {
print("Connected to host : \(host) with Port : \(port)")
socket.readDataWithTimeout(-1, tag: 0)
}
func socket(sock: GCDAsyncSocket!, didReadData data: NSData!, withTag tag: Int) {
print("Data read successfully")
socket.readDataWithTimeout(-1, tag: 0)
}

How to Call Web Service in Web Socket in 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()
}
}
}

Resources