简体   繁体   中英

java socket tips regarding storing the socket

I have create one chat application in java. Front end I made as Eclipse RCP. Earlier I created one view and there it shows how many users are logged. Now I created another view which is anther plugin project and I want to do differnt activities in that view. So is that okay if I created one session concept in my chat application where key will be username and value will be the socket. Is this a good approch? Because I want to get the socket object inside the newly created object. I dont want to create the new socket object.

Is there an alternative? If you need the socket in two views, then you will have to keep them in a central place and you will need to give them a key that you can pass around to the views. So the approach sounds reasonable to me.

But maybe it would be better to wrap the sockets in a helper class that implements methods that your views need. This way, you can keep all the common code in a single place.

What is more important: If you ever need to change the chat protocol, you can now do it in your helper class and the views don't have to change.

To do that, turn your manager into an OSGi service and let Eclipse inject it into your views. See Build lightweight OSGi applications with Eclipse

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM