Skip to content

allow public access to socket and keep it open when requested - #471

Open
Bersaelor wants to merge 1 commit into
httpswift:stablefrom
Bersaelor:feature/stateful_websockets
Open

allow public access to socket and keep it open when requested#471
Bersaelor wants to merge 1 commit into
httpswift:stablefrom
Bersaelor:feature/stateful_websockets

Conversation

@Bersaelor

Copy link
Copy Markdown

Hey there, I've been using swifter for a while, but in my case I was always keeping the sockets open, to allow for stateful connections.
In my case communication is bidirectional, the phone acts as a sort of remote for the browser window.

The below are all the changes I needed from this project, would this be something worth merging into the main branch?

@michaelenger

michaelenger commented Sep 28, 2021

Copy link
Copy Markdown
Contributor

Hey @Bersaelor, could you give me an example of how you're using this?

Also, could you fix the merge conflict while you're at it?

@michaelengermichaelenger self-assigned this Sep 28, 2021
@Bersaelor

Bersaelor commented Sep 29, 2021

Copy link
Copy Markdown
Author

hey @michaelenger ,

from a UX perspective the user only opens the page in their browser and then never interacts with the desktop pc/mac anymore, instead going across the room and controlling the website with their phone.

First I save the socket that is currently open when the website loads, like this:

 // during init
do{try httpServer.start()}catch{
log.error("While starting httpserver: \(error)")}httpServer["/"]=htmlHandler(filePathComponent:"/Web/index.html", context: rootContext)httpServer["/websocket-echo"]=websocket(
text:webSocket(session:text:),
binary:nil, pong:nil, connected:nil, disconnected:nil)}fileprivatefunc webSocket(session:WebSocketSession, text:String){guard text.hasPrefix("BTI$")else{
// if text doesn't start with 'Browser To Iphone', then it's not a socket message
session.writeText(text)return}
log.debug("message: \(text)")ifself.currentSocket ==nil{self.socketDidOpen()
currentSocket = session.socket
session.socket.didClose ={[weak self]inself?.socketDidClose()self?.currentSocket =nil}}guardlet message =RemoteScreenMessage(message: text)else{
log.error("text couldn't be parsed: \( text )")return}
log.debug("Message: \(message)")DispatchQueue.main.async{self.received(message)}}

and then when I want to make the currently presented desktop page change I do:

fileprivatefunc sendToClient(_ message:MessageToBrowser){letmsgString="....."
log.debug("Attempting to send msg: \(msgString)")
networkingQueue.async{guardlet socket =self.currentSocket else{return}letsession=WebSocketSession(socket)
session.shouldCloseSocket =falseif message.type.shouldBeRepeated {self.lastMessage = message }
session.writeText(msgString)}}

@michaelenger

Copy link
Copy Markdown
Contributor

This is a cool idea, what do you think @Vkt0r?

@michaelengermichaelenger removed their assignment Jun 14, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@Bersaelor@michaelenger@Vkt0r